![](/img/trans.png)
[英]cast a pointer to member function in derived class to a pointer to abstract member function
[英]Save a pointer to member function of derived class
我正在寻找一种保存指向派生类的成员函数的指针的方法。
举个例子:
class A
{
public:
typedef void (A::*FunctionP) (int a);
};
class B : A
{
public:
void Test(int a)
{
//Do stuff
}
B()
{
FunctionP theFunc = &B::Test;
}
};
以下代码无法编译。还有另一种方法吗? (可能使用模板,也可以使用boost)
(Btw类A是一个抽象类,其实现之一是调用另存为FunctionP的函数)
谢谢
只需使用static_cast
:
FunctionP theFunc = static_cast<FunctionP>(&B::Test);
Coliru: http ://coliru.stacked-crooked.com/a/2cfed4926aed43db
不过,根据您的需求,最好使用std::function
和std::bind
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.