Thread: How to use function ptr in class

  1. #1
    Registered User
    Join Date
    Nov 2005

    How to use function ptr in class

    I have a function

    typedef (bool)(*Callback)(void);
    SystemMgr->SetFrameFunc(Callback func);

    now I have this class
    class GameMgr
      bool CallbackFunc() { return false; }
    If I try to use the GameMgr::CallbackFunc in the SetFrameFunc() function, I get errors saying use &GameMgr::CallbackFunc to get pointer to member, but it does not work. Any help would be greatly appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    A member function pointer is not a function pointer.

    If you have control over the definition of SetFrameFunc(), perhaps you could consider having it take a function object instead, upon which you can use std::mem_fun_ref() to convert the CallbackFunc() member function to a function object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    My Hat of Guessing thinks you're trying to actually use GameMgr::CallbackFunc as a parameter to SetFrameFunc, rather than GameMgrObj.CallbackFunc (i.e., passing the function from a particular object, not the class in general). Also, I'm not 100% convinced the signature will be right (don't member functions have a GameMgr &this implicitly put into their declaration lists? I should check, rather than asking questions).

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    You can write a template class that will take care of this. The template class should take 2 classes. One object is called and one does the calling. The template binds the two together so that they both use 'this' call.

    If you want an example just MSN me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback function as class method
    By schifers in forum Windows Programming
    Replies: 39
    Last Post: 05-19-2008, 03:02 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM