0
Is there any difference between creating new object with the default copy constructor and declaring a reference to it?
Cat cat1; Cat cat2(cat1); // option 1 - using the default copy constructor Cat &rCat2 = cat1; // option 2 - using reference
1 ответ
0
cat2 is just copy of cat1. rCat2 is reference so if you change the object rCat2 cat1 is also changed.
If you change something in cat2 the data is not going to change in cat1.
cat1.x = 0;
cat2.x = 3;
cout << cat1.x << " " << cat2.x;
// output: 0 3
cat1.x = 0;
rCat2.x = 3;
cout << cat1.x << " " << rCat2.x;
// output: 3 3