Saturday, 24 August 2013

C++ Functor overloading typedef to function pointer

C++ Functor overloading typedef to function pointer

I have this code:
Typedef to function pointer
typedef bool(*tMyFunc)(tSomeTypeDef);
The function pointer is used to declare a member var to point to a
callback function in an Obj-C class
@interface MyClass : NSObject
{
tSomeTypeDef _someValue;
tMyFunc _callback;
}
- (void) registerNotificationWithCallback:(tMyFunc)callback;
@end
@implementation MyClass
- (void) registerNotificationWithCallback:(tMyFunc)callback {
_callback = callback;
}
- (void) didSomething:(NSNotification*)notification {
if (_callback)
_callback(_someValue);
}
@end
In a C++ class, I want to pass a functor in place of a tMyFunc function
pointer
class MyOtherClass {
MyClass* m_instance;
public:
bool operator ()(tSomeTypeDef val);
}
MyOtherClass::MyOtherClass()
{
//... init m_instance, then register callback as follows
[m_instance registerNotificationWithCallback:*this];
}
bool MyOtherClass::operator ()(tSomeTypeDef val)
{
return false;
}
It compile with the error
No viable conversion from 'MyOtherClass' to 'tMyFunc' (aka
'bool(*)(tSomeTypeDef)')

No comments:

Post a Comment