Sign in to follow this  
johnnyBravo

c++ Possible for function pointers to point to member function?

Recommended Posts

BeanDog    1065
The short answer is no. Functions in classes are not constructed in memory the same way as static (global) functions. There is extra data passed to the function to describe the object the function is to act upon. So if you have a library that gives a typedef'ed callback that you want to fill with a non-static member function, it's simply not possible.

If you're designing your own callbacks, though, the long answer is yes, according to Google.

Share this post


Link to post
Share on other sites
anonuser    148
It is possible. With a little magic.

friend a method, that'll help.
Or have a static method and make sure a pointer to this is passed in as an argument and call the regular method.

A great example of this is done at stromcode's Win32 tutorial. It completely encapsulates Win32 windows. Including the WinProc. Which is a callback method.

Stromcode's C++ Win32

Share this post


Link to post
Share on other sites
Deception666    182
You can have a member function pointer. The syntax is a bit strange but it can be done. To do this, you have to have a valid instance of the object for it to work.

Example:
typedef void (SomeClass::*MemFuncPtr)( void );

MemFuncPtr pMemFuncPtr = &SomeClass::SomeFunction;

SomeClass oSomeClass;

(oSomeClass.*pMemFuncPtr)();

SomeClass * pSomeClass = new SomeClass;

(pSomeClass->*pMemFuncPtr)();

Share this post


Link to post
Share on other sites
Sutekh    122
You must realize what the member function’s purpose is, which is to perform some action on that class or using that class's data. So if the function is not static, the function needs a class object to act on while being executed.

This can be done several ways. You may use a functor, an object which wraps a member function pointer, a pointer to an object, and overloads operator(). Another way to do this is to use a bind, available with boost::bind and boost::function.

Just something I threw together, I hope it helps.
Btw everything I have stated is just stuff I have picked up along the way.


typedef boost::function< int( int, int ) > ClassAFunc;

class A
{
public:
int Add( int a, int b )
{
return a + b;
}
};

int main()
{
//uses a shared smart pointer so that the binded function can never have hanging pointer
boost::shared_ptr< A > ptr( new A);


ClassAFunc func;
// boost::bind<returnType>( functionPointer, objectPointer, argument1, argument2);
//argument1 and 2 simply show where the arguments go when the binded function is called
func = boost::bind<int>( &A::Add, ptr, _1, _2 );

std::cout << func( 1, 2 ); // would print 3
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this