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

26th Nov 2016, 3:29 PM
Marcell Juhasz
1 Odpowiedź
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
26th Nov 2016, 4:22 PM
Isko