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);

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

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);
}

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.