Calling C++ member functions via a function pointer

Read this for detail : // 1 define a function pointer and initialize to NULL int (TMyClass::*pt2ConstMember)(float, char, char) const = NULL; // C++ class TMyClass { public: int DoIt(float a, char b, char c){ cout << “TMyClass::DoIt”<< endl; return a+b+c;}; int DoMore(float a, char b, char c) const { cout << “TMyClass::DoMore” << endl; … Read more

How does the C code that prints from 1 to 1000 without loops or conditional statements work?

Don’t ever write code like that. For j<1000, j/1000 is zero (integer division). So: (&main + (&exit – &main)*(j/1000))(j+1); is equivalent to: (&main + (&exit – &main)*0)(j+1); Which is: (&main)(j+1); Which calls main with j+1. If j == 1000, then the same lines comes out as: (&main + (&exit – &main)*1)(j+1); Which boils down to … Read more

Function Pointers in Java

The Java idiom for function-pointer-like functionality is an an anonymous class implementing an interface, e.g. Collections.sort(list, new Comparator<MyClass>(){ public int compare(MyClass a, MyClass b) { // compare objects } }); Update: the above is necessary in Java versions prior to Java 8. Now we have much nicer alternatives, namely lambdas: list.sort((a, b) -> a.isGreaterThan(b)); and … Read more

How can I pass a member function where a free function is expected?

There isn’t anything wrong with using function pointers. However, pointers to non-static member functions are not like normal function pointers: member functions need to be called on an object which is passed as an implicit argument to the function. The signature of your member function above is, thus void (aClass::*)(int, int) rather than the type … Read more

C isn’t that hard: void ( *( *f[] ) () ) ()

There is a rule called the “Clockwise/Spiral Rule” to help find the meaning of a complex declaration. From c-faq: There are three simple steps to follow: Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or [] => Array X size … Read more

Callback functions in Java

If you mean somthing like .NET anonymous delegate, I think Java’s anonymous class can be used as well. public class Main { public interface Visitor{ int doJob(int a, int b); } public static void main(String[] args) { Visitor adder = new Visitor(){ public int doJob(int a, int b) { return a + b; } }; … Read more

Using generic std::function objects with member functions in one class

A non-static member function must be called with an object. That is, it always implicitly passes “this” pointer as its argument. Because your std::function signature specifies that your function doesn’t take any arguments (<void(void)>), you must bind the first (and the only) argument. std::function<void(void)> f = std::bind(&Foo::doSomething, this); If you want to bind a function … Read more

Why do function pointer definitions work with any number of ampersands ‘&’ or asterisks ‘*’?

There are a few pieces to this that allow all of these combinations of operators to work the same way. The fundamental reason why all of these work is that a function (like foo) is implicitly convertible to a pointer to the function. This is why void (*p1_foo)() = foo; works: foo is implicitly converted … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)