C++ Function Pointer Problems

This is a discussion on C++ Function Pointer Problems within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm having problems understanding/using function pointers. I'm trying to use a member function of an instantiated class i.e. non-static ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    24

    C++ Function Pointer Problems

    Hello,

    I'm having problems understanding/using function pointers. I'm trying to use a member function of an instantiated class i.e. non-static as a call back function and need to pass a function pointer to the caller.

    I've posted an example below which will result in the same compile error in my code :

    Code:
    class MyClass {
    
    public:
    	MyClass(){};
    	virtual ~MyClass(){};
    	int add(int oneNumber, int twoNumber){return (oneNumber + twoNumber);};
    };
    
    int main(){
    	MyClass* t = new MyClass();
    	int (*fp)(int,int);	// Create a function pointer - will point to a member function of an INSTANCE of class MyClass
    	fp = t->add;		// Results in a compiler error
    	delete(t);
    }
    The compiler gives :

    test.cpp:12: error: argument of type ‘int (MyClass::)(int, int)’ does not match ‘int (*)(int, int)
    Just how do I pass a (*) pointer ? (and what is a (*) pointer anyway ???) Unfortunately I cannot change the calling function as it is code not written by myself and it is has been written in C. How do I pass a pointer to a function of a class instance ?

    I'm using Linux by the way and gcc/g++ compiler

    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm having problems understanding/using function pointers. I'm trying to use a member function of an instantiated class i.e. non-static as a call back function and need to pass a function pointer to the caller.

    You can't. The member function requires a pointer to *this, so logically it's type is closer to:

    int ( * )( MyClass*, int, int )

    >> Unfortunately I cannot change the calling function as it is code not written by myself and it is has been written in C.

    The only way to do it then would be a function that uses a global variable, eg:

    Code:
    int add_callback( int a, int b )
    {
    	// myClassObject is defined elsewhere in the program
    	return myClassObject.add( a, b ); 
    }
    Another option, and this is a bit of a hack:

    Code:
    int add_callback_hack( int a, int b )
    {
    	static MyClass* ptr = 0;
    	if( a == numeric_limits< int >::max( ) )
    		ptr = ( MyClass* )b;
    	return ptr->add( a, b ); 
    }
    
    int main(){
    	MyClass mc;
    	add_callback_hack(numeric_limits<int>::max(), int(&mc)); 
    	int (*fp)(int,int) = add_callback_hack;
    }
    EDIT: I honestly wouldn't recommend the hack, though, as it's error-prone and could potentially lead to some nasty bugs!

    EDIT#2: Ok, so I've come up with a much better hack, and it's totally safe.

    Code:
    int add_callback_hack( int a, int b, MyClass* instance )
    {
    	static MyClass* 
    		ptr = 0;
    	if( instance )
    		ptr = instance;
    	return ptr->add( a, b ); 
    }
    
    inline void add_callback_init( MyClass* instance )
    {
    	add_callback_hack( 0, 0, instance );
    }
    
    int add_callback( int a, int b )
    {
    	return add_callback_hack( a, b, 0 ); 
    }
    
    int main(){
    	MyClass mc;
    	add_callback_init(&mc); 
    	int (*fp)(int,int) = add_callback;
    }
    Last edited by Sebastiani; 07-28-2009 at 05:59 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Just make add() static.

    As Sebastiani points out, in the general case this cannot be done, because most methods need a pointer the their associated object to work with. Static methods do not, however.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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