Pointer to member function

This is a discussion on Pointer to member function within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Pointer to member function

    Hi,

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

    The test with pointers to non-member function works fine:
    Code:
    void callOut( void (*callback)() )
    {
    	callback();
    }
    
    void testOut()
    {
    	cout << "testOut hier" << endl;
    }
    
    int main(int argc, char **argv)
    {
    	callOut( &testOut );
    }

    Now if I try it with a member function:

    Code:
    class A
    {
    public:
    	void out()
    	{
    		cout << "A here " << endl;
    	}
    }; 
     
    void callOut( void (A::*callback)() )
    {
    	callback();
    	// error: must use ‘.*’ or ‘->*’ to call pointer-to-member
    	// function in ‘callback (...)’
    }
    
    int main(int argc, char **argv)
    {
    	A* a = new A;
    	callOut( &a->out );
    	// error: ISO C++ forbids taking the address of a bound
    	// member function to form a pointer to 
    	// member function. Say ‘&A::out’
    }
    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!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    to call a member function you need an object.
    e.g. like this
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
    	void out()
    	{
    		cout << "A here " << endl;
    	}
    }; 
     
    void callOut( A*a, void (A::*callback)() )
    {
    	(a->*callback)();
    }
    
    int main(int argc, char **argv)
    {
    	A* a = new A;
    	callOut( a, &A::out );
    }
    Kurt

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thank you both, worked great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21