I believe Constructors
and Destructors
in base class
cannot be inherited by derived classes
of the base class. Is my understanding correct.

- 6,405
- 21
- 76
- 127
5 Answers
Your understanding is correct. For example, if you have
class Base
{
Base(int i) {}
};
class Derived: public Base {};
Derived d(3);
This will not compile because the Base constructor is not inherited. Note that default and copy constructor are created by the compiler if possible, and call the corresponding constructor of base classes, therefore for those constructors it looks as if those were inherited.

- 19,311
- 3
- 39
- 64
I think this is what you are looking for? You can call the superclass constructor by adding the following to your class constructor
SubClass(int foo, int bar)
: SuperClass(foo)
A full example can be found here What are the rules for calling the superclass constructor?
-
Thanks @Niels. But i just want to know are the constructors and destructors inherited? – nitin_cherian Nov 12 '11 at 10:19
-
2@LinuxPenseur: No, they aren't. Each class has their own constructor and destructor (that includes copy constructor). If you don't provide a constructor or destructor, the compiler will generate them for you by default. – cpx Nov 12 '11 at 10:28
On the contrary, each constructor of a derived class calls a constructor of the base [super-] class. Each destructor of a derived class is called just before the destructor of the base [super-] class.
Inheritance concerns classes, not functions or constructors.

- 223,805
- 18
- 296
- 547
-
1Inheritance does very much concern functions, because they are the ones that get inherited. – Björn Pollex Nov 12 '11 at 11:02
-
Class-es are inherited much more than member functions. member functions can be specialized (e.g. redefined, and called with the virtual indirection). – Basile Starynkevitch Nov 12 '11 at 11:17
-
A class inherits all functions from its bases. You can treat them as if the sub-class itself had defined them. It inherits the functions. You can also call non-virtual functions inherited from the base-class. – Björn Pollex Nov 12 '11 at 11:21
-
I thought that the correct terminology is that the class inherit from all its base classes. – Basile Starynkevitch Nov 12 '11 at 11:23
-
A class inherits from its bases, but what does it inherit from them - the members. – Björn Pollex Nov 12 '11 at 11:31
No, they are inherited. Destructors, for one, are always inherited and called in the order reverse to the creation. For example if we have classes foo
, bar
and xyzzy
:
class foo { ... };
class bar : public foo { ... };
class xyzzy : public bar { ... };
Then if you destoy an object of class xyzzy
, destructors will be called in the following order: ~xyzzy()
, ~bar()
and finally ~foo()
.
Constructors are also always inherited, but they cannot be called directly. You have to use them in the constructor initialization list or the default constructor will be called (default constructor is the one that takes no arguments).For example, say we have following classes:
class foo {
public:
foo();
foo (int _value);
}
class bar : public foo {
int m_value;
public:
bar (int _value);
}
bar::bar (int _value)
{
m_value = _value;
}
In this case, when you create an object of class bar
, a constructor for foo
is invoked, but it's the default constructor (foo()
). Constructor that takes an argument foo (int _value)
is never called. But if we changed definition of the bar (int _value)
constructor to this:
bar::bar (int _value)
: foo (256 - _value), m_value (_value)
{
}
Then instead of a default constructor, foo (int _value)
will be called.

- 9,425
- 13
- 50
- 81
-
2See your answer contradicts with `cpx`'s comment below. I believe what `cpx` tells is right. Could you please verify @Septagram – nitin_cherian Nov 12 '11 at 10:46
-
-1: [Constructors are not inherited](http://stackoverflow.com/questions/347358/), and the same goes for destructors. – Björn Pollex Nov 12 '11 at 11:01
-
what if destructor in not virtual and you are deleteting some base class pointer? – joy Nov 12 '11 at 11:04
-
1This is answer is informative. Is the problem with it simply that it incorrectly uses the term "inherited"? – Barend Venter Jan 23 '15 at 09:23
As this question explains, constructors are not inherited. The same applies to destructors.

- 1
- 1

- 75,346
- 28
- 201
- 283