Copy constructor

I'm trying to wrap my head around copy constructors...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class A
{
    public:
        int x;
};

int main()
{
    A a1;
    a1.x = 1000;
    cout << a1.x << endl; // output: 1000
    A a2 = a1;

    
    /*-----------------------------------------------------------------------------
     *  What is the main difference between
     *                                                  A a2(a1);
     *                                           and
     *                                                  A a2 = a1;
     *-----------------------------------------------------------------------------*/

    a2.x+=200;
    cout << a2.x << endl; // output: 1200

    return 0;
}


Last edited on
http://en.cppreference.com/w/cpp/language/initialization

"you wrap your head" is more a statement than a question.

Constructor initializes an object.
Copy constructor initializes an object with values copied from an another object of same type.
Topic archived. No new replies allowed.