Constructor and Destructor Inheritance
Asked Answered
R

5

8

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

Radford answered 12/11, 2011 at 10:10 Comment(0)
A
5

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.

Amerind answered 12/11, 2011 at 11:14 Comment(0)
V
1

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?

Vast answered 12/11, 2011 at 10:15 Comment(2)
Thanks @Niels. But i just want to know are the constructors and destructors inherited?Radford
@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.Wallace
L
0

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.

Lazar answered 12/11, 2011 at 10:17 Comment(5)
Inheritance does very much concern functions, because they are the ones that get inherited.Acosmism
Class-es are inherited much more than member functions. member functions can be specialized (e.g. redefined, and called with the virtual indirection).Lazar
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.Acosmism
I thought that the correct terminology is that the class inherit from all its base classes.Lazar
A class inherits from its bases, but what does it inherit from them - the members.Acosmism
A
0

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.

Addax answered 12/11, 2011 at 10:26 Comment(4)
See your answer contradicts with cpx's comment below. I believe what cpx tells is right. Could you please verify @AddaxRadford
-1: Constructors are not inherited, and the same goes for destructors.Acosmism
what if destructor in not virtual and you are deleteting some base class pointer?Uropod
This is answer is informative. Is the problem with it simply that it incorrectly uses the term "inherited"?Ripleigh
H
0

As this question explains, constructors are not inherited. The same applies to destructors.

Housekeeping answered 12/11, 2011 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.