virtual function call from base class

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

    g() of derived class will be called if in member function.

    g() of base class will be called if in constructor or destructor.

    https://www.geeksforgeeks.org/calling-virtual-methods-in-constructordestructor-in-cpp/

    // calling virtual methods in constructor/destructor
    #include 
    using namespace std; 
    
    class dog 
    { 
    public: 
        dog()  
        { 
            cout<< "Constructor called" <

    output:

    Constructor called
    Virtual method called
    Derived class Constructor called
    Derived class Virtual method called
    Virtual method called
    

提交回复
热议问题