How Can I Pass a Member Function to a Function Pointer?

You can’t. You either pass a pointer to a static method or Parent has to accept also a pointer to the object. You might want to look at boost::bind and boost::function for that: #include <boost/bind.hpp> #include <boost/function.hpp> struct Y { void say(void) { std::cout << “hallo!”;} boost::function<void()> getFunc() { return boost::bind(&Y::say, this); } }; struct … Read more

Print address of virtual member function

Currently there is no standard way of doing this in C++ although the information must be available somewhere. Otherwise, how could the program call the function? However, GCC provides an extension that allows us to retrieve the address of a virtual function: void (A::*mfp)() = &A::func; printf(“address: %p”, (void*)(b->*mfp)); …assuming the member function has the … Read more

How to hash and compare a pointer-to-member-function?

All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try: bool (Class::*fn_ptr)() = &Class::whatever; const char *ptrptr = static_cast<const char*>(static_cast<const void*>(&fn_ptr)); Now treat ptrptr as pointing to an array of (sizeof(bool (Class::*)())) bytes, and hash or compare those bytes. You can use unsigned char … 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

tech