Alex F. February 2016

C++ copy costructor

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;
          

Answers


John_West February 2016

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) :
  

Post Status

Asked in February 2016
Viewed 1,267 times
Voted 10
Answered 1 times

Search




Leave an answer