C ++ Functor将typedef重载为函数指针

时间:2013-08-25 01:29:45

标签: c++ overloading function-pointers objective-c++ functor

我有这段代码:

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

0 个答案:

没有答案