6

I wonder why people say:

"Inheriting class doesn't inherit the constructor".

If you could CAN use the parent class' constructor, and the parameterless constructor are called automatically no matter what.

Example:

#include <iostream>
using namespace std;

class A {
    private :
        int x;
    public  :
        A () {
            cout << "I anyway use parameter-less constructors, they are called always" << endl;
        }
        A (const int& x) {
            this->x = x;
            cout << "I can use the parent constructor" << endl;
        }
};

class B : public A {
    private :
        int y;
    public  :
        B() {

        }
        B (const int& x, const int& y) : A (x) {
            this->y = y;
        }
};

int main() {

    B* b  = new B(1,2);
    B* b1 = new B();
    return 0;
}

http://ideone.com/e.js/6jzkiP

So is it correct to 'say', constructors are inherited in c++ ? What is exact definition of "inherit" in programming languages ?

Thanks in advance.

Paweł Brewczynski
  • 2,665
  • 3
  • 30
  • 43

5 Answers5

6

I wonder why people say: "Inheriting class doesn't inherit the constructor".

Perhaps it is best to illustrate this with an example:

struct Foo
{
  Foo(int, int) {}
};

struct Bar : Foo
{
};

What it means is that there is no Bar::Bar(int, int) constructor that you can call, despite the existence of a constructor with the same parameter list in the base class. So you cannot do this:

Bar b(42, 42);

In C++11, you can actually inherit constructors, but you must be explicit about it:

struct Bar : Foo
{
  using Foo::Foo;
};

Now, you can say Bar b(42, 42);

JBL
  • 12,588
  • 4
  • 53
  • 84
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
5

What they mean is that constructor signatures are not inherited.

In your example, B does not have a constructor taking a single const int& even though its base class does. In this sense it has not "inherited" the constructor (but can still make use of it).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

I think what they mean is:

struct A {
    A(int, int) { }
};

struct B : public A {
};

int main()
{
    A a(1, 2); // ok
    B b(1, 2); // error
}

To compare with “non-special” member functions:

struct A {
    void f() { }
};

struct B : public A {
};

int main()
{
    A a;
    B b;
    a.f(); // ok
    b.f(); // ok too
}

But of course, from within B you can call accessible A constructors (as automatically generated ones do). Ditto for the destructor.


Note that in C++11 you can use the “inheriting constructors” feature:

struct A {
    A(int, int) { }
};

struct B : public A {
    using A::A;
};

int main()
{
    A a(1, 2); // ok
    B b(1, 2); // ok now
}
gx_
  • 4,690
  • 24
  • 31
2

A derived class can/must see base class constructors in order to invoke them, for consistency. However, their signature is not exposed in the derived class, hence, one cannot construct the class without an explicitly defined constructor, which forwards the required arguments to the base class constructor.

In C++11, one can inherit constructors: What is constructor inheritance?

Another approach to circumvent 'proxy constructors' in C++ < 11: How to define different types for the same class in C++

Community
  • 1
  • 1
Sam
  • 7,778
  • 1
  • 23
  • 49
0

In your example, the default ("parameterless" as you say) constructor of B does invoke the default constructor of A, but this does not mean that B "inherited" that constructor, only that it "has access to" it. That is, A's default constructor is accessible from within B, yet it is not accessible from outside (no one can use A's default constructor from outside to construct an instance of B).

Another way to look at it is to ask, what is something frustrating about constructors and inheritance in C++? A common answer from some people (including myself) would be that there is no automatic facility which allows "pass-through" construction of base classes taking arguments from derived class constructors without explicitly declaring and defining the latter.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436