Skip to main content
gamedev.net
🔒 Locked

Member function pointers

Started by Mantear 2 replies 400+ views
Original Post
Mantear
Mantear
Greetings, Using C++/MSVS 2005 Express. I've got a class where I want to be able to call a multitude of different member functions based on certain conditions. I want to avoid a massive switch statement and use function pointers. Most of the examples I've googled for have shown have to create and use member function pointers, but not within the class that contains the member function. Example:
class Foo
{
   public:
      Foo();
      ~Foo();

      void Bar();

      void (Foo::*funcPtr)(void);
};

Foo::Foo()
{
   funcPtr = &Foo::Bar; // <-- This compiles ok.
   (*funcPtr)(); // <-- Cannot get this syntax correct.
}
Thanks in advance.
Hollower
Hollower
(*this.*funcPtr)();
Mantear
Mantear
Thanks, that works!
Beer Hunter
Beer Hunter
Or (this->*funcPtr)(). The thing to know is that .* and ->* are special operators made for this purpose.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.