1

Im trying to get a good grasp with copy constructors & I've found this part of code.

 #include<iostream>
    using namespace std;
    class A1 {
        int data;
    public:
        A1(int i = 10) :
                data(i) {
            cout << "I am constructing an A1 with: " << i << endl;
        }
        A1(const A1& a1) :
                data(a1.data) {
            cout << "I am copy constructing an A1" << endl;
        }
        ~A1() {
            cout << "I am destroying an A1 with: " << data << endl;
        }
        void change() {
            data = data * 10;
        }
    };
    class A2 {
        int data;
    public:
        A2(int i = 20) :
                data(i) {
            cout << "I am constructing an A2 with: " << i << endl;
        }
        A2(const A2& a2) :
                data(a2.data) {
            cout << "I am copy constructing an A2" << endl;
        }
        ~A2() {
            cout << "I am destroying an A2 with: " << data << endl;
        }
        void change() {
            data = data * 20;
        }
    };
    class A3 {
    public:
        A3() {
            cout << "I am constructing an A3" << endl;
        }
        A3(const A3& a3) {
            cout << "I am copy constructing an A3" << endl;
        }
        ~A3() {
            cout << "I am destroying an A3" << endl;
        }
        void change() {
            cout << "Nothing to change" << endl;
        }
    };
    class A {
        A1 a1;
        A2 a2;
        A3 a3;
    public:
        A() {
            cout << "I am constructing an A" << endl;
        }
        A(const A& a) :
                a1(a.a1) {
            cout << "I am copy constructing an A" << endl;
        }
        ~A() {
            cout << "I am destroying an A" << endl;
        }
        A& operator=(const A& a) {
            cout << "I am performing a stupid assignment between As" << endl;
            if (this != &a)
                a1 = a.a1;
            return *this;
        }
        void change() {
            a1.change();
            a2.change();
            a3.change();
        }
    };
    class BigA {
        A data1;
        A& data2;
    public:
        BigA(A& a) :
                data1(a), data2(a) {
            cout << "I just constructed a BigA" << endl;
        }
        ~BigA() {
            cout << "I am destroying a BigA" << endl;
        }
        A get(int index) {
            if (index == 1)
                return data1;
            else
                return data2;
        }
    };
        BigA volta(BigA& biga)
     //BigA& volta(BigA& biga)
            {
        cout << "Volta ta data?" << endl;
        return biga;
    }
    int main() {
        A first;
        BigA biga(first);
        volta(biga).get(2).change();
        return 0;
    }

However,I can't understand why I get these results.Especially,why A1 and A copy constructor are called and not constructor and I don't get at all when volta function is called(The results enclosed by ****) :

I am constructing an A1 with: 10
I am constructing an A2 with: 20
I am constructing an A3
I am constructing an A
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
I just constructed a BigA
****
Volta ta data?
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A
Nothing to change
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 400
I am destroying an A1 with: 100
I am destroying a BigA
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 20
I am destroying an A1 with: 10
****
I am destroying a BigA
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 20
I am destroying an A1 with: 10
I am destroying an A
I am destroying an A3
I am destroying an A2 with: 20
I am destroying an A1 with: 10

EDIT_AssignmentOperatorQuery : If I add this function in BigA

void change() {
    A& rdata1 = data1;
    A cdata2 = data2;
}

and call it from main : biga.change(); Why instead of the default assignment operator , the copy-constructor and constructor is being called and I get

I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A

EDIT_AnsweringMyOwnQuery : I just found out that this is initialization by copy constructor and not assignment by assignment operator.

Alex R.
  • 459
  • 5
  • 16

1 Answers1

1

Let's start with it.

A first;

You create A object, its fields (non-static members) are initialized

"Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished."

I am constructing an A1 with: 10
I am constructing an A2 with: 20
I am constructing an A3

And your version of constructor without parameters is being called:

I am constructing an A

When you write

BigA biga(first);

one of your BigA constructors is invoked. It takes a reference to A object, so, first is not copied (reference is set when providing a value).

Then, member initializer lists time comes,

BigA(A& a) :
            data1(a), data2(a)

and data1 is of type A, the first object is copied (it is referenced here as a)

A new A object is created by its own copy constructor. At first, it calles copy constructor for A1,

A(const A& a) :
a1(a.a1)

I am copy constructing an A1

Then, A's a2 and a3 fields are default-initialized.

I am constructing an A2 with: 20
I am constructing an A3 

The body of copy constructor for A1 is executed then:

I am copy constructing an A

Let's return to BigA initialization. We spoke about data1 initialization until now, and now the time for A& data2:

BigA(A& a) :
            data1(a), data2(a)

As it is reference, and the reference being passed to initialize it, it is just an assignment, no output.

BigA constructor (that takes A&) body is executed then:

I just constructed a BigA

Now, we would try to clarify what happens on

volta(biga).get(2).change();

This function is being called:

BigA volta(BigA& biga)
{
    cout << "Volta ta data?" << endl;
    return biga;
}

Again, pass by reference results in no copy constructor call.

We have function body being executed:

"Volta ta data?"

The function returns unnamed object of class BigA, so copy constructor should be called.

You have not provided copy constructor like BigA (const BigA & biga), so the default copy constructor is being invoked. It does sequential member initialization of A data1; and then A& data2;

The first member is initialized by copying data1 field of your unnamed object, thus copy constructor of A is being called. What it is printed here is explained above (see: A new A object is created by its own copy constructor...)

I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A

Then, get method runs with index == 2

A get(int index) {
        if (index == 1)
            return data1;
        else
            return data2; // <--- this line is executed

data2 is A&, and the method returns A, this leads to A copy constructor execution.

I am copy constructing an A1
I am constructing an A2 with: 20
I am constructing an A3
I am copy constructing an A

At last, change runs

void change() {
        a1.change();
        a2.change();
        a3.change();
    }

and only a3.change() prints something:

Nothing to change

On program termination

The destruction occurs in the reverse order, and the last created change'd object is destroyed at first.

I am destroying an A
I am destroying an A3
I am destroying an A2 with: 400
I am destroying an A1 with: 100

I am destroying a BigA is printed twice, but I just constructed a BigA - only once. The latter is due to you have no copy constructor for BigA that takes const & BigA (it is also pointed out above).

Answering your query

void change() {
    A& rdata1 = data1;
    A cdata2 = data2;
}
//in the main():
biga.change();

Yes, you're right that the copy constructor would be called here A cdata2 = data2; because the object cdata2 is previously uninitialized. This is the case well explained under this ref.

If you change the code like that

A cdata2;
cdata2 = data2;

you would see the expected assignment:

I am constructing an A1 with: 10
I am constructing an A2 with: 20
I am constructing an A3
I am constructing an A
I am performing a stupid assignment between As
Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
  • Thanks for the super analytical explanation it was more that I could ask.However a problem came up (I Edited my question) with the assignment operator (or so I think) and I would be grateful if you could explain that as well. – Alex R. Feb 08 '16 at 15:00
  • 1
    It;s interesting that initialization order depends not on initialization list sequence but [on the declaration order in the class](http://stackoverflow.com/a/1242835). [It is required because](http://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order#comment1069647_1242835) class can have multiple constructors, and the only destuctor should destroy fields in definite reverse order. – John_West Feb 08 '16 at 19:06