I came across a situation that can be summarized in the following code snippet. Basically, I would like my classes to inherit constructors. It works, but compilation fails as soon as I have a virtual method defined in C
. Apparently the compiler deleted the constructor.
My questions are:
#include <iostream>
struct A {
A() = delete;
A(int x) : x_(x) {}
int x_;
};
struct B : public A {
};
struct C : public B {
// What? defining virtual method kills the inherited constructor
//virtual void foo() {}
};
int main() {
C c{10};
std::cout << "Hello World: " << c.x_ << std::endl;
}
You can call a virtual function in a constructor, but be careful. It may not do what you expect. In a constructor, the virtual call mechanism is disabled because overriding from derived classes hasn't yet happened. Objects are constructed from the base up, “base before derived”.
As a general rule, you should never call virtual functions in constructors or destructors. If you do, those calls will never go to a more derived class than the currently executing constructor or destructor. In other words, during construction and destruction, virtual functions aren't virtual.
Virtual Function in C++When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax. For example: class Base {
Constructors are inherited only when you explicitly declare the inherited constructors. This is done as follows:
struct B : public A {
using A::A;
};
struct C : public B {
using B::B;
};
In your program, you are not using inherited constructors; you are using aggregate initialization. Adding a virtual function makes C
no longer an aggregate, so aggregate initialization cannot be used. If you declare inherited constructors, then you can construct C
without having to use aggregate initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With