Item 32. Preventing Copying

Access specifiers (public, protected, and private) can be used to express and enforce higher-level constraints on how a type may be used.

The most common of these techniques is to disallow copying of an object by declaring its copy operations to be private and not defining them:


It’s necessary to declare the copy constructor and copy assignment operator, since otherwise the compiler would declare them implicitly, as public inline members. By declaring them to be private, we forestall the compiler’s meddling and ensure that any use of the operations—whether explicit or implicit—will result in a compile-time error: ...

Get C++ Common Knowledge: Essential Intermediate Programming 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.