virtual function call from base class

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

    The g of the derived class will be called. If you want to call the function in the base, call

    Base::g();
    

    instead. If you want to call the derived, but still want to have the base version be called, arrange that the derived version of g calls the base version in its first statement:

    virtual void g() {
        Base::g();
        // some work related to derived
    }
    

    The fact that a function from the base can call a virtual method and control is transferred into the derived class is used in the template method design pattern. For C++, it's better known as Non-Virtual-Interface. It's widely used also in the C++ standard library (C++ stream buffers for example have functions pub... that call virtual functions that do the real work. For example pubseekoff calls the protected seekoff). I wrote an example of that in this answer: How do you validate an object’s internal state?

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 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<iostream> 
    using namespace std; 
    
    class dog 
    { 
    public: 
        dog()  
        { 
            cout<< "Constructor called" <<endl; 
            bark() ; 
        } 
    
        ~dog() 
        {  
            bark();  
        } 
    
        virtual void bark() 
        {  
            cout<< "Virtual method called" <<endl;  
        } 
    
        void seeCat()  
        {  
            bark();  
        } 
    }; 
    
    class Yellowdog : public dog 
    { 
    public: 
            Yellowdog()  
            { 
                cout<< "Derived class Constructor called" <<endl;  
            } 
            void bark()  
            { 
                cout<< "Derived class Virtual method called" <<endl;  
            } 
    }; 
    
    int main() 
    { 
        Yellowdog d; 
        d.seeCat(); 
    } 
    

    output:

    Constructor called
    Virtual method called
    Derived class Constructor called
    Derived class Virtual method called
    Virtual method called
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 17:37

    It is the Derived::g, unless you call g in Base's constructor. Because Base constructor is called before Derived object is constructed, Derived::g can not logically be called cause it might manipulate variables that has not been constructed yet, so Base::g will be called.

    0 讨论(0)
  • 2020-12-12 17:39

    I think you trying to invent Template Method Pattern

    0 讨论(0)
提交回复
热议问题