How to use function ptr in class

This is a discussion on How to use function ptr in class within the C++ Programming forums, part of the General Programming Boards category; I have a function typedef (bool)(*Callback)(void); SystemMgr->SetFrameFunc(Callback func); now I have this class Code: class GameMgr { public: bool CallbackFunc() ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    How to use function ptr in class

    I have a function

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


    now I have this class
    Code:
    class GameMgr
    {
    public:
      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
    Posts
    5,006
    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
    Location
    Singapore
    Posts
    22,272
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    14,185
    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
    Posts
    9,598
    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, 04:02 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  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, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21