virtual function call from base class

前端 未结 9 1852
故里飘歌
故里飘歌 2020-12-12 17:12

Say we have:


Class Base
{   
    virtual void f(){g();};
    virtual void g(){//Do some Base related code;}
};

Class Derived : public Base
{   
    virtual         


        
9条回答
  •  孤街浪徒
    2020-12-12 17:32

    Well... I'm not sure this should compile. The following,

    Base *pBase = new Derived;
    

    is invalid unless you have:

    Class Derived : public Base
    

    Is it want you meant? If this is want you meant,

    pBase->f();
    

    Then the call stack would go like this:

    Derived::f()
        Base::f()
            Derived::g()
    

提交回复
热议问题