如果我声明:
class Avoidance : public Schema<std_msgs::String,prog1::Command>{
我尝试着
void*(Schema<std_msgs::String,prog1::Command>::*pt)();
pt=&Avoidance::frontBusy;
编译器报告我
error: cannot convert ‘void* (Avoidance::*)()’
to
‘void* (Schema<std_msgs::String_<std::allocator<void> >, prog1::Command_<std::allocator<void> > >::*)()’ in assignment
为什么?回避继承自
Schema<std_msgs::String,prog1::Command>
然后回避 IS 模式<.....>
最佳答案
这不是成员函数指针的工作原理。如果frontBusy
是基函数,则需要适本地键入指针。不过,调度仍将按预期工作!
这是一个基本示例:
struct A { virtual void f() = 0; };
struct B : A { virtual void f() { } };
void dispatch(void (A::*pf)(), A & a)
{ // ^^^^^
(a.*pf)();
}
int main()
{
B x;
dispatch(&A::f, x); // calls x.B::f()
} // ^^^^^
所以,根据您的情况,您想要:
void (Schema<std_msgs::String,prog1::Command>::*p)()
= &Schema<std_msgs::String,prog1::Command>::frontBusy;
关于C++ 模板和带有方法指针的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14401761/