liveBook · Manning

Copy assignment operator

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

A copy assignment operator is a C++ special member function that assigns the state of one existing object to another existing object of the same or compatible class.

A copy assignment operator assigns values from one already initialized object to another already initialized object. Unlike a copy constructor, it overwrites an existing target rather than creating a new object. section 8.5

Treat these details as practical correctness checks:

  • The usual signature returns a reference, accepts a const reference, updates the target, and returns *this; returning an object creates unnecessary temporaries. section 9.4
  • Compiler-generated assignment performs a member-wise copy, which is suitable only when that behavior preserves the class invariant. section 8.5
  • Pointer members can make member-wise copying share memory; dynamic resources, handles, connections, sockets, and files may require deep copying or deliberate ownership transfer. section 8.5 section 8.6
  • Guard resource-owning assignment against self-assignment before deleting or replacing the target’s resource. section 9.5
  • In a derived class, assign the base-class subobject as well as the derived members. section 8.7

Deep-copying an owning buffer

An InputReader owning a dynamically allocated filename array cannot safely rely on the compiler’s member-wise assignment: two objects would share the same array. A correct operator copies each filename into the target’s existing storage, updates the count, and returns *this. If capacities differ, it must replace the target storage before copying. This preserves independent state and avoids the segmentation fault caused by shallow copying. section 8.5

Self-assignment with an owning pointer

An operator that deletes engine and then reads car.engine fails when mustang = mustang. Check this == &car first, return *this for the same object, and only then replace the resource. section 9.5

Use =default when compiler-generated behavior is correct and =delete when assignment is intentionally forbidden. When a class owns resources, implement the copy operation consistently with its copy constructor and destructor, or use ownership-managing members where appropriate. section 2.4 section 7.6

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

How deep it goes
Explains target state, member-wise copying, resource ownership, self-assignment, inheritance, return references, and default or deleted operators.
Examples
Uses containers, file readers, owning buffers, communication channels, derived students, complex numbers, and self-assigned automobiles.
What it assumes
Assumes familiarity with C++ classes, constructors, destructors, pointers, inheritance, and dynamically allocated resources.
What you can do after
Apply it via correctly written C++ operators that copy resources, support chaining, handle self-assignment, and update base-class state.
How it connects
Connects copy constructors, destructors, class invariants, shallow copy, deep copy, move assignment, inheritance, and dynamic resource management.

Also explained in

Compare how 2 books explain copy assignment operator →

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.