The issue with the Copy constructor

Reference of pointers from c1 and c2 and pointing to the same memory address, the first block of the array we created when c1 is created.

Deep Copy

Whenever we use a copy constructor, we need to be careful that if there are any dynamic memory allocation in Heap in C++. If that is the case, we need to write our own copy constructor as the default copy constructor in C++ will only perform a shallow copy.

The reference value of the backpack is now the difference.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Cheung

Kevin Cheung

𝑾𝒉𝒂𝒕 𝑰 𝒄𝒂𝒏𝒏𝒐𝒕 𝒄𝒓𝒆𝒂𝒕𝒆, 𝑰 𝒅𝒐 𝒏𝒐𝒕 𝒖𝒏𝒅𝒆𝒓𝒔𝒕𝒂𝒏𝒅. . An ordinary developer records his life and learning progress.