Lodec
Lodec

Reputation: 87

Understand some specific cases of the constructor

I have the following code :

#include <iostream>
using namespace std;
class X {
public:
    X () {
        x = 0;
        cout << "1";
    }
    X (int x) {
        this->x = x;
        cout << "2";
    }
    X (double x) {
        this->x = x;
        cout << "3";
    }
    X (const X& x) {
        this->x = x.x;
        cout << "4";
    }
protected:
    int x;
};
class Y : public X {
public:
    Y () : X(10) {
        cout << "5";
    }
    Y (int p) : X(p) {
        cout << "6";
    }
    Y (const X& x) : X(x) {
        cout << "7";
    }
    Y (const X& x1, X x2) : X(x1), x(x2) {
        cout << "8";
    }
protected:
    X x;
};

int main() {
    Y y1;
    cout << endl;
    Y y2(10);
    cout << endl;
    Y y3(y1);
    cout << endl;
    Y y4(y2, y3);
    cout << endl;
}

The output of this code is :

215
216
44
4448

PS : I apologize for my English mistakes and I thank the people who correct me

Upvotes: 0

Views: 37

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596713

Y derives from X, and also contains a data member x of type X. When instantiating an object instance of Y, an X constructor must be called for both the inherited X portion of the Y class, and the x data member.

In the first 2 cases, the Y constructors you are calling are not explicitly initializing the x data member, so the compiler uses the default X() constructor to initialize it implicitly. That is where the 1 comes from.

In the 3rd case, 7 is not output because you are not constructing the y3 object with an X object as input. You are constructing it with another Y object. But you haven't explicitly defined a Y(const Y&) copy constructor, so the compiler defines an implicit copy constructor for you, which invokes the X copy constructor twice, once for the inherited X portion of the Y class, and once for the x data member.

In the last case, you see 4 being output 3 times because you are invoking the X copy constructor 3 times, two explicitly and one implicitly:

  • when passing x1 to X()
  • when passing y3 to x2 (which slices y3, because x2 is pass-by-value)
  • when passing x2 to x()

Upvotes: 2

Related Questions