virtual function call from base class

前端 未结 9 1841
故里飘歌
故里飘歌 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:34

    As you have defined g() to be virtual, the most derived g() will be looked up in the vtable of the class and called regardless of the type your code is currently accessing it.

    See the C++ FAQ on virtual functions.

提交回复
热议问题