为什么我不能将指向Derived类成员函数的指针强制转换为类Base?

Lor*_*one 5 c++ member-function-pointers

对我来说,将a转换void(Derived::*)()为a 看起来非常安全void(Base::*)(),就像在这段代码中一样:

#include <iostream>
#include <typeinfo>
using namespace std;
struct Base{
    void(Base::*any_method)();
    void call_it(){
        (this->*any_method)();
    }
};
struct Derived: public Base{
    void a_method(){
        cout<<"method!"<<endl;
    }
};
int main(){
    Base& a=*new Derived;
    a.any_method=&Derived::a_method;
    a.call_it();
}
Run Code Online (Sandbox Code Playgroud)

但编译器抱怨演员a.any_method=&Derived::a_method;.这是一个阻止细微编程错误的障碍,还是一些让编译器编写者生活更轻松的东西?是否有变通方法让Base类具有指向Derived 无类型知识的成员函数的指针(也就是说,我不能Base使用模板参数创建模板Derived).

Mat*_*Mat 7

如果您Derived::a_method()尝试使用数据成员只出现在一个对象(或从一个对象派生但与之无关的对象)DerivedBase,而不是在其中,那么会发生什么?BaseBaseDerived

相反的转换是有道理的,这个没有.