Function Pointers to Function Members

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kardon33
    New Member
    • May 2007
    • 158

    Function Pointers to Function Members

    Hello Bytes,

    I am trying to use function pointers in program with two classes.

    Here's my setup:

    In Class A I have a public method defined as [int doSomthing(int a) ].

    In Class B I need a method to set a callback for a method [ int callback(int)]

    This all works well if the callback function i give is not a member of class A or any other class.

    A.cpp:187: error: no matching function for call to ‘B::setTrigger( int (A::*)(int))’
    B.h:81: note: candidates are: void B::setTrigger(i nt (*)(int))


    Do I need to specify in class B that the call back needs to be from class A or is there a way to make it more dynamic so when I want class C to set the callback it doesn't break.


    Here is an overview of the code
    Code:
    class A  {
    public:
     int doSomthing(int i);
    }
    class B  {
    public:
        typedef int (*Trigger)(int level);
        Trigger TriggerCallback; // This is what I set with setTrigger
        void setTrigger(int (*callback)(int responce));
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to specify the correct class using the scope resolution operator and then correctly apply the member dereferencing operators .* or this->* depending upon whether you have an object or the address of an objct.

    Here is your corrected code to a point that it compiles.

    Code:
    class A  { 
    public: 
     int doSomthing(int i); 
    }; 
    
    int A::doSomthing(int i)
    {
       return 1;
    }
    class B  { 
    public: 
    	//typedef int (A::*Trigger)(int level); 
        //Trigger TriggerCallback; // This is what I set with setTrigger 
    
    	int (A::*TriggerCallback)(int level);
    	void setTrigger(int (A::*TriggerCallback)(int level));
    };
    
    void B::setTrigger(int (A::*pf)(int level))
    {
        TriggerCallback = pf;
    }
    
    int main()
    {
    	A objA;
    	B objB;
    
    	objB.setTrigger(&A::doSomthing);
    
    }

    Comment

    • kardon33
      New Member
      • May 2007
      • 158

      #3
      Thanks for the reply that will do it.

      However is it possible to have Class B be more vague when setting up the triggers.

      What I mean by that is if I want say Class C to set Class B's trigger then I would need two Class B set trigger methods and their respected variables. One with

      int (A::*TriggerCal lbackA)(int level);
      void setTriggerA(int (A::*TriggerCal lbackA)(int level));

      and another with

      int (C::*TriggerCal lbackC)(int level);
      void setTriggerB(int (C::*TriggerCal lbackC)(int level));


      Is there a way you can specify any type of class? Maybe using templates?

      I thought about using static methods in my Class A and C but for my application I need to access my class object in the callback.
      Last edited by kardon33; Oct 1 '10, 04:37 PM. Reason: Opps

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is starting to look like the Observer design pattern.

        In this pattern, an object (the Observer) registers to be notified if the state of some other object changes. The changing object then excutes a Notify() method to the observer.

        Often, a Mediator object acts as a registry for Observers and the changing object just notifies the Mediator. The Mediator notifies the registered observers. This allows the changing object to deal with only one Mediator but still allows for many obervers.

        Comment

        Working...