Im reading this article on constructors for c++
We recommend that you be careful when you call virtual functions in constructors. Because the base class constructor is always invoked before the derived class constructor, the function that's called in the base constructor is the base class version, not the derived class version. In the following example, constructing a DerivedClass causes the BaseClass implementation of print_it() to execute before the DerivedClass constructor causes the DerivedClass implementation of print_it() to execute:
the example:
class BaseClass {
public:
BaseClass() {
print_it();
}
virtual void print_it() {
cout << "BaseClass print_it" << endl;
}
};
class DerivedClass : public BaseClass {
public:
DerivedClass() {
print_it();
}
virtual void print_it() {
cout << "Derived Class print_it" << endl;
}
};
int main() {
DerivedClass dc;
}
Here's the output:
BaseClass print_it
Derived Class print_it
I tried this code and the output is as stated above. However I also tried the same example without the virtual keyword:
class BaseClass {
public:
BaseClass() {
print_it();
}
void print_it() {
cout << "BaseClass print_it" << endl;
}
};
class DerivedClass : public BaseClass {
public:
DerivedClass() {
print_it();
}
void print_it() {
cout << "Derived Class print_it" << endl;
}
};
int main() {
DerivedClass dc;
}
So what is the difference and what is the danger they are warning for?
@marked as duplicate:
This question is different as the consturctors both call the virtual method instead of one constructor calling the virtual method.