I'm trying to create some type of callback system for events. I have a function where I'm attempting to set a member to a pointer to a function but I'm receiving an error. This is what I attempted to do:

void (MyClass::*test)() = &MyClass::TestCallback;
    EnterButton->SetCallback(test);

And then in the class declaration for "MyClass" I put this:

void SetCallback(void (MyClass::*callback)());

But I'm getting this error: error: expected ')' before '::' token|

anyone have any ideas?

Hm.. not quite. I understand how to work with pointers to normal functions. I even understand how to work with pointers to methods. The confusion arises when I try to pass the method pointer as a parameter.

edit: I found this example dealing with method pointers (but not passing them as parameters):

class Foo
{
        public:
                double One( long inVal );
                double Two( long inVal );
};

void main( int argc, char **argv )
{
	double (Foo::*funcPtr)( long ) = &Foo::One;

 	Foo aFoo;

 	double result =(aFoo.*funcPtr)( 2 ); 
 
	return 0;
}

So I thought making a function like..

void SetFuncPtr(void (Foo::*funcPtr)(long));

would work. However I was getting errors as posted above. I tried including a template like..

template<class Foo> void SetFuncPtr(void (Foo::*funcPtr)(long));

but still no avail. At that point I was getting another error.. no matching function for call to..

I seem to have overcome the problem by defining the interface and implementation in the same file. However I've come across another issue.. I currently do this:

template <class Client> void SetMouseClickCallback(void (Client::*callback)()) {
            MouseClickCallback = callback;
        }

However, MouseClickCallback looks like this:

void (Client::*MouseClickCallback)();

which won't work because again, Client isn't defined. So I'm unsure as to how I can store this..

This seems unnecessary.. Shouldn't I just be able to use the Client type if I include the header file? I have included the header file that has the class interface but I still get 'Client' has not been declared|

Edit:

I was able to solve this problem using forward-declaration!

I know you solved the problem, but may I suggest a more elegant method IMO. This is a typical case where an interface class and multiple inheritance could do very well:

//Callback.h
class IMouseClickCallback {
  public:
    virtual void MouseClickCallback() = 0;
};

//Client.h
class Client : public some_other_base_if_any, public IMouseClickCallback {
  private:
    ...
  public:
    ...
    virtual void MouseClickCallback(); //with implementation in cpp
    ...
};

//wherever_else.cpp
#include "Callbacks.h" //no need for forward-declarations, just include the header of the interface.

void SetMouseClickCallback(IMouseClickCallback* Obj);
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.