[英]Save a pointer to member function of derived class
I am looking for a way to save a pointer to a member function of a derived class. 我正在寻找一种保存指向派生类的成员函数的指针的方法。
For exemple: 举个例子:
class A
{
public:
typedef void (A::*FunctionP) (int a);
};
class B : A
{
public:
void Test(int a)
{
//Do stuff
}
B()
{
FunctionP theFunc = &B::Test;
}
};
The following code dose not compile.. Is there another way to do this? 以下代码无法编译。还有另一种方法吗? (Using template's maybe, or boost)
(可能使用模板,也可以使用boost)
(Btw class A is an abstract class witch one of its implementations is invoking functions saved as FunctionP) (Btw类A是一个抽象类,其实现之一是调用另存为FunctionP的函数)
Thank you 谢谢
Just use a static_cast
: 只需使用
static_cast
:
FunctionP theFunc = static_cast<FunctionP>(&B::Test);
Coliru: http://coliru.stacked-crooked.com/a/2cfed4926aed43db Coliru: http ://coliru.stacked-crooked.com/a/2cfed4926aed43db
Still, it might be even better to use std::function
and std::bind
, depending on your needs. 不过,根据您的需求,最好使用
std::function
和std::bind
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.