954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to pass a function pointer as an argument?

I have to pass a pointer to a callback function. When the callback function is not a member function in my class, the argument &callback works. When I make the callback function a member function, I get a compiler error: "error c2276 '&' : illegal operation on bound member function expression." I am using Visual C++ .net and need to make the callback a member function so the data can be displayed to the gui. Thanks.

intf->registerForMessage(Message_ID1, &callback);

DotNetUser
Junior Poster in Training
69 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Make the call back function to friend for both the classes.

kianbabu
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Usually it's pretty handy to typedef your function pointers.

typedef void (*FuncType)(int, bool);

//...

void SomeFunction( FuncType foo, int n ){ }


For class member function pointers, you will need a pointer to the instance of the object on which you wish to call the function, as well as the pointer to the member function.

Which is a slightly confusing way to say that you pass the "this" pointer of your class instance along with the function pointer, then use something like: (*pObj).foo(100,false); where pObj is the "this" pointer.

You can use templates to simplify adding callbacks, or you can avoid needing the "this" pointer by making the function static.

I've used templates where I want to add a callback to a class of a type I don't know yet, but has a member function with a specified prototype.

An example of the easy workaround is:

typedef void (*FuncType)();
class Bar
{
public:
	static void ffoo(){ }
};

void addCallback( FuncType foo )
{

}

int main()
{
	addCallback(Bar::ffoo);
}
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 
I have to pass a pointer to a callback function.


Is there any reason you can not use a functor? Whit a functor you have a great deal of flexibility in how you manage the context and you dont have to change the format or signature of your class to do things.

For instance, in the general case, a functor works just like a callback function:

#include <iostream>

struct Functor {
    void operator ()(int i) { std::cout << "Callback("<<i<<")" << std::endl; }
};      
    
void method (int i, Functor& f) {
    f(i);
}

int main () {
    Functor func;
    method (42,func);
    return 0;
}


However, in your case it seems as though you need the private class members to populate a form of some sort at which point you have two options:
- implement operator() in your class and pass that class around
- wrap the class you want in a functor

The functor wrapper approach would look something like

#include <iostream>

struct Foo {
    void use(int x) {
        std::cout << "Foo::use(" << x << ") " << std::endl;
    }
};
    
template< class T >
struct Functor {
    T& t_;
    Functor (T& t) : t_(t) {}
    void operator ()(int i) {
        t_.use (i);
    }
};

template< class T >
void method (int i, Functor< T >& f) {
    f(i);
}

int main () {
    Foo foo;
    Functor< Foo > func(foo);
    method (42,func);
    return 0;
}



Where, now, you can carry the class around in the functor and delegate to the instance as you need.

L7Sqr
Practically a Master Poster
656 posts since Feb 2011
Reputation Points: 201
Solved Threads: 123
 

Excellent idea L7Sqr! I'll have to remember that.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You