Thread: syntax to pass a member function pointer to another class?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    50

    syntax to pass a member function pointer to another class?

    hey,

    i'm having trouble working out what the syntax is when you want to pass a member function of a class (from a another member function of that class) to another function (which is a member of another class)...

    like this:
    Code:
    typedef void(*FnToCallback)(void);
    
    class B
    {
    public:
    void Register(FnToCallback passedInFnToCallback);
    void FnThatCallsBackLater();
    
    private:
    FnToCallback storedFnToCallback;
    };
    
    
    class A
    {
    public:
    A(B classToRegisterTo);
    void CallbackFunction();
    };
    
    B instanceOfB;
    A instanceOfA(instanceOfB);
    
    A::A(B classToRegisterTo)
    {
        	classToRegisterTo.Register(CallbackFunction);
    }
    
    void A::CallbackFunction()
    {
    //does something useful
    }
    
    
    void B::Register(FnToCallback passedInFnToCallback)
    {
     storedFnToCallback = passedInFnToCallback;
    }
    
    void B::FnThatCallsBackLater()
    {
    storedFnToCallback()
    }
    
    int main()
    {
    //lots of code
    instanceOfB.FnThatCallsBackLater();
    //more code
    }
    i think that makes sense

    anyway, as it stands, i get an error like this:
    cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

    i think the problem is that i am trying to pass a pointer to a member function, because if i create a test global function and try to pass that, it works.

    thanks in advance to anyone who can decipher all that and help
    Last edited by reanimated; 11-27-2003 at 03:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. Function pointer to class member?
    By no-one in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2002, 08:23 PM