我有这个代码:
typedef 到函数指针
typedef bool(*tMyFunc)(tSomeTypeDef);
函数指针用于声明成员 var 以指向 Obj-C 类中的回调函数
@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
在 C++ 类中,我想传递一个仿函数来代替 tMyFunc 函数指针
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;
}
它编译时出现错误
从“MyOtherClass”到“tMyFunc”(又名“bool(*)(tSomeTypeDef)”)没有可行的转换