可能是一个幼稚的c++
问题,
我在一个具有一个公共typedef
函数的库中有一个类,该函数也是其构造函数的一个参数。我希望能够通过为此typedef创建一个定义来调用此构造函数。
为了简化理解,我在下面创建了示例代码,
namespace example {
class MyClass {
public:
typedef int(*MyFunc)(char c, int x);
MyClass(MyFunc myFunc) {
}
MyClass() {
}
};
}
static bool MyFuncDefinition(char c, int x) {
return 0;
}
int client() {
example::MyClass *myClassObject = new example::MyClass(MyFuncDefinition);
return 0;
}
这会引发编译器错误,
Error 1 error C2664: 'example::MyClass::MyClass(const example::MyClass &)' : cannot convert argument 1 from 'bool (__cdecl *)(char,int)' to 'example::MyClass::MyFunc' d:\anoopelias\wspace\ppc3\experiments\test\typedefexample.cpp 24 1 TypeDefTest
什么是正确的方法来做到这一点?
简而言之,您的职能不遵守所提供的合同(布尔不是int)。
尝试如下:
namespace example {
class MyClass {
public:
typedef int(*MyFunc)(char c, int x);
MyClass(MyFunc myFunc) {
}
MyClass() {
}
};
}
static int MyFuncDefinition(char c, int x) {
return 0;
}
int main() {
example::MyClass *myClassObject = new example::MyClass(MyFuncDefinition);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句