[英]How Can I Pass a Member Function to a Function Pointer?
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
}
};
如何将成员函数传递给函数指针,然后如何调用该函数指针?
你不能 您要么传递指向静态方法的指针,要么Parent必须接受指向该对象的指针。
您可能需要查看boost :: bind和boost :: function的功能 :
#include <boost/bind.hpp>
#include <boost/function.hpp>
struct Y
{
void say(void) { std::cout << "hallo!";}
boost::function<void()> getFunc() { return boost::bind(&Y::say, this); }
};
struct X
{
//non-boost:
void (Y::*func)();
Y* objectY;
void callFunc() { (objectY->*func)(); }
//boost version:
boost::function<void()> f;
};
X x;
Y y;
x.f = boost::bind(&Y::say, boost::ref(y));
x.f = y.getFunc();
x.f();
x.func = &Y::say;
x.objectY = &y;
x.callFunc();
为了响应您的上一次编辑,要形成指向成员的指针,您必须使用&
和classkey::
。 对于普通函数,没有等效于函数名称的指针到函数隐式转换的函数。
// not valid:
p.funcPointer3=TestFunc;
// valid:
p.funcPointer3 = &Child::TestFunc;
要通过指向成员的指针访问成员,您必须使用.*
或->*
运算符。
例如
(this->*p.funcPointer3)();
随着bind
和Lambda Functions的出现,我们实际上可以在C ++ 11中完成所有funcPointer*
。 让我们先谈谈每个人,然后讨论他们在做什么:
funcPointer
试图在不接收Child
对象的情况下调用Child
方法,因此必须保存Child
对象。 子对象可以通过指针保存: bind(&Child::TestFunc, this)
或在C ++ 14中,可以通过值保存: [arg = *this]() mutable { arg.TestFunc(); }
[arg = *this]() mutable { arg.TestFunc(); }
funcPointer2
试图使用Parent*
调用Child
方法。 我们可以这样做: [](Parent* arg){ static_cast<Child*>(arg)->TestFunc(); }
[](Parent* arg){ static_cast<Child*>(arg)->TestFunc(); }
当然,这不会比(new Parent)->TestFunc()
更合法,因此我们假设Parent*
实际上是Child*
,如果您愿意将Parent
为多态类型 ,则可以验证在调用您的lambda之前: [](Parent* arg) {
assert(dynamic_cast<Child*>(arg) != nullptr);
static_cast<Child*>(arg)->TestFunc();
}
funcPointer3
试图存储一个指向Child
方法的指针,而您已经可以使用该方法了。 您只需要使用一个Child
对象来调用它,例如: (this->*p.funcPointer3)()
。 但是您必须像这样分配funcPointer3
: funcPointer3 = &Child::TestFunc
,因为如果您尝试这样做: funcPointer3 = &TestFunc
将会得到错误: '&':对绑定成员函数表达式的非法操作
接下来,不能使用函数指针或成员函数指针来引用Closure Type ,因此我们需要将函数指针转换为function
对象。 ( funcPointer3
只是一个成员函数指针,所以它并不需要转换,但我会把它转换成要证明一个function
对象可以包含一个成员函数的指针,它简化了呼叫: p.funcPointer(this)
) :
class Parent {
public:
function<void()> funcPointer;
function<void(Parent*)> funcPointer2;
function<void(Child*)> funcPointer3;
};
现在,我们已经适应Parent
,我们可以很容易地在1分配作为证明,2和3:
void Child::Do() {
Parent p;
p.funcPointer = bind(&Child::TestFunc, this);
p.funcPointer2 = [](Parent* arg) { static_cast<Child*>(arg)->TestFunc(); };
p.funcPointer3 = &Child::TestFunc;
p.funcPointer();
p.funcPointer2(this);
p.funcPointer3(this);
}
您可能知道这一点,并且只是在进行测试,但是我们可以像在Child::Do
创建新的Parent
对象一样容易地使用Child
继承自Parent
的成员。 我将其切换为示例,并提供以下代码: http : //ideone.com/yD7Rom
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.