More on the Copy Constructor

Let us look back at the situation. The core of the problems discussed in the previous section is copying an object whose data members are pointers to heap memory.

Each object instance is supposed to point to the area of memory that is allocated specifically for it. For example, class String has a pointer that points to the area of heap memory that contains characters associated with the individual String object.

When data members of one object are copied into data members of another object, the corresponding pointers in both objects will have the same contents. Hence, they will point to the same area of heap memory. These objects can die at different moments in time; for example, the formal value parameter of a function ...

Get Core C++ A Software Engineering Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.