Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ virtual method overriding [duplicate]

Possible Duplicate:
Calling virtual functions inside constructors

main.cpp

#include <iostream>

class BaseClass {

    public:

    BaseClass() {
        init();
    }

    virtual ~BaseClass() {
        deinit();
    }

    virtual void init() {
        std::cout << "BaseClass::init()\n";
    }

    virtual void deinit() {
        std::cout << "BaseClass::deinit()\n";
    }

};

class SubClass : public BaseClass {

    public:

    virtual void init() {
        std::cout << "SubClass::init()\n";
    }

    virtual void deinit() {
        std::cout << "SubClass::deinit()\n";
    }

};

int main() {
    SubClass* cls = new SubClass;
    delete cls;
    return 0;
}

Why is init() and deinit() not properly overriden and the BaseClasses' methods are called instead of the SubClasses ones? What are the requirements to make it work?

BaseClass::init()
BaseClass::deinit()
like image 847
Niklas R Avatar asked Jan 31 '13 13:01

Niklas R


People also ask

Can a virtual method be overridden?

A virtual method can be created in the base class by using the “virtual” keyword and the same method can be overridden in the derived class by using the “override” keyword.

Is it necessary to override a virtual method in C#?

When the method is declared as virtual in a base class, and the same definition exists in a derived class, there is no need for override, but a different definition will only work if the method is overridden in the derived class. Two important rules: By default, methods are non-virtual, and they cannot be overridden.

Is virtual function and method overriding same?

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.

Can virtual methods be inherited?

Base classes can't inherit what the child has (such as a new function or variable). Virtual functions are simply functions that can be overridden by the child class if the that child class changes the implementation of the virtual function so that the base virtual function isn't called. A is the base class for B,C,D.


2 Answers

Because you are calling a virtual method inside a constructor. While constructing Base class the derived one (SubClass) isn't still constructed, so actually it doesn't still exist.

It's generally a good practice to avoid calling virtual methods inside constructors.

like image 71
Heisenbug Avatar answered Sep 28 '22 03:09

Heisenbug


They are overridden just fine.

But you've invoked them from the base constructor, and when the base constructor is executing, the derived part of the object does not yet exist.

So this is a largely a safety feature, and it's mandated by the C++ standard.

like image 21
Lightness Races in Orbit Avatar answered Sep 28 '22 03:09

Lightness Races in Orbit