pass method compatible with ofEvent as a parameter to another method

contrived, I guess.

So I’m trying to add as3 style event dispatching to ofxFlash (https://github.com/julapy/ofxFlash).

In as3, to add an event listener, you use

  
  
sprite->addEventListener( eventString, callbackMethod);  
  

In order to do this I was thinking of doing something like this:

  
  
void ofxFlashEventDispatcher :: addEventListener(string evtType, listenerMethod mtd ){  
   //....  
    ofAddListener(theEvent, this, mtd);  
}  
  

where listenerMethod is the following typedef

  
  
typedef  void (ofxFlashObject::*listenerMethod)(const void*, ofEventArgs &  ); 	  
  

but I get an error: ‘no matching function for call to ofAddListener’, so I understand that the last parameter is not correct, as the other two are definitely fine.

I’ve tried many variants: using and not using a typedef, making the method templated…, but I can’t quite figure out the correct syntax to make ofAddListener accept my passed method.

Any ideas?

thanks!
j

i think the problem can be with the typdef, i remember having trouble with typedefs of memeber function pointers. have you tried to define the function without the typdef:

  
  
void ofxFlashEventDispatcher :: addEventListener(string evtType, void (ofxFlashObject::*listenerMethod)(const void*, ofEventArgs &  ) ){  
    ofAddListener(theEvent, this, listenerMethod);   
}  
  

thanks!

and

oh!

it wasn’t the typedef, but by getting rid of it I discovered where the problem was.
It’s a subtle one, so, for the record.

I was using this method pointer signature

  
  
void (ofxFlashObject::*listenerMethod)(const void*, ofEventArgs &  )  
  

but was calling ofAddListener inside the class ofxFlashEventDispatcher, so in

  
  
ofAddListener(testEvent, this, listenerMethod)  
  

this referred to an instance of ofxFlashEventDispatcher, which is not an ofxFlashObject (even if it’s a subclass, more testing to be done…), so changing the method pointer signature to

  
  
void (ofxFlashEventDispatcher::*listenerMethod)(const void*, ofEventArgs &  )  
  

solves the problem.

public derision ftw

Thanks arturo!
j