liveBook · Manning

Unique object

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

A unique object is an object whose identity or existence must not be duplicated within the modeled domain.

A unique object represents a domain concept that must not be duplicated. Copying such an object violates its class invariant, even when the program compiles and runs normally. section 9.2

Avoid the duplication mistake by checking every copy operation:

  • Hiding only the copy assignment operator is insufficient: the compiler can still provide a default copy constructor. section 9.2

A duplicated artwork

Constructing ArtPiece dup(ml) prints “Mona Lisa” twice: the compiler-created copy constructor silently produces a second object, with no visible forgery warning.

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

How deep it goes
Explains implicit copy construction, private declarations, member-function loopholes, and deleted operations.
Examples
Uses artwork, the Mona Lisa, ArtPiece, accidental copying, and compiler-enforced deletion.
What it assumes
Assumes familiarity with C++ classes, constructors, and assignment operators.
What you can do after
Apply copy-control declarations to prevent accidental duplication of unique objects.
How it connects
Connects class invariants with copy constructors and copy assignment operators.

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.