C++ virtual table layout of MI(multiple inheritance)

前端 未结 2 1604
遇见更好的自我
遇见更好的自我 2020-12-02 17:22

Look at the following C++ code

class Base1 {  
public:  
    Base1();  
    virtual ~Base1();  
    virtual void speakClearly();  
    virtual Base1 *clone()         


        
2条回答
  •  感情败类
    2020-12-02 17:56

    At runtime when you get:

        Base2 b2;
        Base1* b1_ptr = (Base1*)&b2;
        b1_ptr->mumble();    // will call Base2::mumble(), this is the reason.
    

    Then the Base2::mumble() needs to be invoked! Take care that mumble() is the ONLY virtual method that was overriden in hierarchy. (Even, You may think that clone() is overriden too but that returns different type among classes then it is another signature).

提交回复
热议问题