why can't I cast a pointer to Derived class member function to the same but of class Base?

后端 未结 4 648
半阙折子戏
半阙折子戏 2020-12-06 19:48

To me it looks perfectly safe to cast a void(Derived::*)() to a void(Base::*)(), like in this code:

#include 
#incl         


        
4条回答
  •  遥遥无期
    2020-12-06 20:15

    What happens if your Derived::a_method() attempts to use a data member only present in Derived, not in Base, and you call it on a Base object (or an object derived from Base but not related to Derived)?

    The conversion the other way around makes sense, this one doesn't.

提交回复
热议问题