liveBook · Manning

Deep copy

As explained in 100 C++ Mistakes and How to Avoid Them

Deep copy semantics create independent copies of dynamically owned or referenced resources rather than sharing the same resource.

Deep copy semantics create independent copies of dynamically owned resources instead of copying pointers that make source and target share the same memory. Shallow copying can violate the class invariant and cause incorrect behavior when ownership is meant to remain unique. section 8.6

A class owning dynamic resources must coordinate its destructor, copy constructor, and copy assignment operator. The copy constructor creates a new resource from the source; assignment first handles the target’s existing resource, then creates its independent copy. Modern C++ extends this responsibility with move construction and move assignment. section 7.6

The copied Page shares its body

A problematic Page copies body(o.body), so two Page objects refer to one TextSection. The corrected copy constructor uses new TextSection(*(o.body)); copy assignment deletes the existing body before allocating its independent copy, and the destructor releases it. A modernized version uses std::unique_ptr and defaulted move operations. section 7.6

Listing 7.7 A copy constructor that shares unique resources
class TextSection {
    // assume a clever implementation
};
 
class Page {
private:
    TextSection* headers;                                      #1
    TextSection* body;
public:
    Page(TextSection* h) : headers(h), body(new TextSection()) {}
    Page(const Page& o) : headers(o.headers), body(o.body) {}  #2
    Page& operator=(const Page&);
};
 
int main() {
    Page p1(new TextSection());
    Page p2 = p1;
    return 0;
}

Compare the sharing copy constructor with the uniqueness-preserving implementation in the corrected listing. section 7.6

Deep copying is not always the right ownership operation. A unique resource, such as a limited printer or an endpoint-bound communication channel, may need transfer rather than duplication; after transfer, the source no longer owns or uses it. section 8.6

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains shallow copying, independent allocation, cleanup before assignment, and the big 3/5, but stops short of general ownership design.
Examples
Uses Page and TextSection pointers, communication channels with queued messages, limited printers, and std::unique_ptr implementations.
What it assumes
Assumes pointers, dynamic allocation, and classes, while teaching why default memberwise copying can share pointed-to memory.
What you can do after
Build it from scratch by implementing independent copies, safe assignment cleanup, destruction, and modern move operations.
How it connects
Connects deep copy with class invariants, copy constructors, copy assignment, destructors, move semantics, smart pointers, and unique resources.

Also explained in

Compare how 5 books explain deep copy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.