473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function Pointers to Function Members

158 100+
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. }
  11.  
  12.  
Oct 1 '10 #1
3 1249
weaknessforcats
9,208 Expert Mod 8TB
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. }; 
  5.  
  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 
  14.  
  15.     int (A::*TriggerCallback)(int level);
  16.     void setTrigger(int (A::*TriggerCallback)(int level));
  17. };
  18.  
  19. void B::setTrigger(int (A::*pf)(int level))
  20. {
  21.     TriggerCallback = pf;
  22. }
  23.  
  24. int main()
  25. {
  26.     A objA;
  27.     B objB;
  28.  
  29.     objB.setTrigger(&A::doSomthing);
  30.  
  31. }
Oct 1 '10 #2
kardon33
158 100+
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
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

1
by: John Silicon | last post by:
Has anyone ever successfully implimented passing member-functions to a varargs function? I thought it would be fairly straight-forward, but seems to come up with nice syntax errors. I've checked...
1
by: Christian Nolte | last post by:
I want to implement dynamic calling of interface methods using pointers to members and have some trouble to get it right. An example of what I am talking about looks like that: ...
7
by: OvErboRed | last post by:
I know you can do: int (MyClass::*ptr)(int) = &MyClass::func; cout << (myObj.*ptr)(5); to point to a member function of class MyClass. But how would you go about making this generic (i.e.,...
3
by: Jakob Vesterstrom | last post by:
Hi all I have question regarding function passing. I have class A class A { public: ... void attach(double f(double in)) {
2
by: rashmi | last post by:
Hi All, ************************************************* error: variable `hdlc_cdevsw' has initializer but incomplete type *************************************************** This is the error...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
5
by: mancomb | last post by:
Hi, I'm curious to the syntax of calling member functions through pointers of classes returned through the -operator. For example (excuse the crude incomplete code); Here are the classes: ...
5
by: Gary Wessle | last post by:
Hi I have a group of functions which have the same signature. void fun_n(void); according to a conditional structure "be it if-else or switch-case" I get to choose which one to run. ...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
2
by: Rahul | last post by:
Hi, I planning to replace the switch-case with an array of function pointers, the index for the array would be the case integers. Each case block would be implemented as a seperate function. It...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.