【发布时间】:2021-11-29 10:29:44
【问题描述】:
class Child;
class Parent
{
public:
void (*funcPointer)();
void (*funcPointer2)(Parent* _this);
void (Child::*funcPointer3)();
};
class Child: public Parent
{
public:
void TestFunc(){
}
void Do(){
Parent p;
p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)'
p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)'
p.funcPointer3=TestFunc; //this works
p.funcPointer3=&Child::TestFunc; // this works too.
p.funcPointer3(); // error, term does not evaluate to a function taking 0 arguments
}
};
如何将成员函数传递给函数指针,然后如何调用该函数指针?
【问题讨论】:
-
@TJCrowder 您可以在此处找到更正后的 C++ 常见问题解答链接:isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr 但我再次认为这无关紧要,因为它警告了成员函数指针的 传递,不是使用成员函数指针。
标签: c++ methods function-pointers member-function-pointers delayed-execution