Sign in to follow this  
Asheh

pointers to member functions

Recommended Posts

Asheh    354
Ive come accross a problem and I know theres ways to solve it but I just cant figure one out.. Basically I have a base (IEventReciever) which other classes derive from( the game states) and each of the game states have an event reciever as a member function. So basically I need to point the current event reciever to the right game state, but I cant figure out how to point to member functions. Can anyone help me out here?

Share this post


Link to post
Share on other sites
Fruny    1658

class Foo
{
public:
int foo(double) { return 0; }
};

int (Foo::* pmf)(double);
pmf = &Foo::foo;

Foo f;
Foo* pf = &f;

(f.*pmf)(25.0);
(f->*pmf)(40.0);




Or use std::mem_fun, boost::mem_fn, boost::bind or boost::signal - depending on your needs.

Note that pointers to members are contravariant. A pointer to a member function of a derived class can be made to point to member functions of the base class - not the other way round.

Share this post


Link to post
Share on other sites
Rog    122
It sounds to me like you would be better to make the event reciever a pure virtual function of the IEventReciever base class. When you derive a game state you implement the virtual function for that specific state. That way C++ will make sure the correct function was called.

Share this post


Link to post
Share on other sites
Washu    7829
Quote:
Original post by Fruny
*** Source Snippet Removed ***

Or use std::mem_fun, boost::mem_fn, boost::bind or boost::signal - depending on your needs.

Note that pointers to members are contravariant. A pointer to a member function of a derived class can be made to point to member functions of the base class - not the other way round.

Ahem...
(pf->*pmf)(40.0);

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