Virtual function calls in constructor and destructor

    class Base
    {
    public:
        Base(){Foo();}
        ~Base(){Foo();}
        virtual void Foo(){std::cout<<"base";}
    };

    class Derived: public Base
    {
    public:
        Derived(){Foo();}
        ~Derived(){Foo();}
        void Foo(){std::cout<<"derived";}
    };

      //main
     {
         Derived d;
     }

Any idea why this code prints “base” and “received”?
I understand that the advice is not to put virtual function calls inside a constructor or descriptor, I just want to know why the code above will have a behavior. Thanks

0
source share
1 answer

C . , , .. C. virtual , C. , C, , , C.

, ! , .

+4

Source: https://habr.com/ru/post/1653998/


All Articles