By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Function Pointers to Function Members

P: 158
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(int (*)(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
Expand|Select|Wrap|Line Numbers
  1. class A  {
  2. public:
  3.  int doSomthing(int i);
  4. }
  5. class B  {
  6. public:
  7.     typedef int (*Trigger)(int level);
  8.     Trigger TriggerCallback; // This is what I set with setTrigger
  9.     void setTrigger(int (*callback)(int responce));
  10. }
Oct 1 '10 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
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.

Expand|Select|Wrap|Line Numbers
  1. class A  { 
  2. public: 
  3.  int doSomthing(int i); 
  4. }; 
  6. int A::doSomthing(int i)
  7. {
  8.    return 1;
  9. }
  10. class B  { 
  11. public: 
  12.     //typedef int (A::*Trigger)(int level); 
  13.     //Trigger TriggerCallback; // This is what I set with setTrigger 
  15.     int (A::*TriggerCallback)(int level);
  16.     void setTrigger(int (A::*TriggerCallback)(int level));
  17. };
  19. void B::setTrigger(int (A::*pf)(int level))
  20. {
  21.     TriggerCallback = pf;
  22. }
  24. int main()
  25. {
  26.     A objA;
  27.     B objB;
  29.     objB.setTrigger(&A::doSomthing);
  31. }
Oct 1 '10 #2

P: 158
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::*TriggerCallbackA)(int level);
void setTriggerA(int (A::*TriggerCallbackA)(int level));

and another with

int (C::*TriggerCallbackC)(int level);
void setTriggerB(int (C::*TriggerCallbackC)(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.
Oct 1 '10 #3

Expert Mod 5K+
P: 9,197
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.
Oct 1 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.