Is calling base class constructor always necessary in C++?
-
02-10-2019 - |
Question
Suppose I have some class C
, and I inherit from it and name this class D
. Do I always have to call C
's default constructor as in this example:
class C {
public:
C() { ... }
};
class D {
public:
D() : C() { ... }
};
Note that C
has only the default constructor. Do I have to call it from D? I couldn't figure out how to find this out.
Thanks, Boda Cydo.
Solution
You don't need to specify the base class constructor in your derived type constructor's initializer list. When it is omitted an attempt will be made to call the base constructor with no parameters. If no such parameterless base constructor exists, then you'll get a compiling error.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow