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

