473,387 Members | 1,578 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,387 software developers and data experts.

Pointer to member function?

1
Hi,

I'm trying to pass pointers to member functions around in my code.

The test with pointers to non-member function works fine:
Expand|Select|Wrap|Line Numbers
  1. Code:
  2.  
  3. void callOut( void (*callback)() )
  4. {
  5.     callback();
  6. }
  7.  
  8. void testOut()
  9. {
  10.     cout << "testOut hier" << endl;
  11. }
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     callOut( &testOut );
  16. }
  17.  


Now if I try it with a member function:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     void out()
  5.     {
  6.         cout << "A here " << endl;
  7.     }
  8. }; 
  9.  
  10. void callOut( void (A::*callback)() )
  11. {
  12.     callback();
  13.     // error: must use ‘.*’ or ‘->*’ to call pointer-to-member
  14.     // function in ‘callback (...)’
  15. }
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     A* a = new A;
  20.     callOut( &a->out );
  21.     // error: ISO C++ forbids taking the address of a bound
  22.     // member function to form a pointer to 
  23.     // member function. Say ‘&A::out’
  24. }

So gcc says it's impossible to use pointers to member function. But I found some tutorials talking about them (but didn't understood them )

Is it possible? And if yes could you please correct my example code?

Background is I'm implementing a finite state machine and the output function of the states are virtual methods of subclasses of an ABC, so pointers to member functions (of the correct derived type) seemed to do the job.
If you say pointer to member fuctions doesn't work in C++, do you know some better idiom to use for this purpose?

tank you very much!
Mar 25 '07 #1
1 7563
horace1
1,510 Expert 1GB
try
Expand|Select|Wrap|Line Numbers
  1. //  AmemberFunc points to a member of A that takes ()
  2. typedef  void (A::*AmemberFunc)();
  3.  
  4. // pass reference to A and pointer to member function
  5. void callOut(A &a, AmemberFunc pFunc )
  6. {
  7.     (a.* pFunc)();   // call member function
  8. }
  9.  
and calling it so
Expand|Select|Wrap|Line Numbers
  1.     A a;
  2.     callOut( a, &A::out );
  3.  
have a read thru
http://www.parashift.com/c++-faq-lite/pointers-to-members.html
Mar 25 '07 #2

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

Similar topics

9
by: David Hill | last post by:
Hello - I am using a library that takes a function pointer as an argument. Is the code below not possible? int library_func(void (*func)(int, short, void *)); I am trying to do this... ...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.