Jump to content
  • Advertisement
Sign in to follow this  
Asheh

pointers to member functions

This topic is 4994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!