Cpp Coding Standards/CLSINIT/ObvCopy

From Apache OpenOffice Wiki
Jump to: navigation, search

Obvious Copyability



Make it obvious, if the class is intended to be copied. Forbid copying otherwise.


There are three possibilities:

  1. The default copying behaviour as provided by the compiler is correct. In this case it may be useful to provide a comment that explicitely states this.
  2. The class shall be copied, but the default behaviour is not correct. This is the case, if the class has members (like pointers that administrate heap memory) that do not copy correctly with the default behaviour. In this case implement copy constructor and operator=().
  3. The class is not intended to be copied. Make this explicit by
    • either deriving from boost::noncopyable
    • or declaring (but not implementing) copy constructor and operator=() private.


If the class is an abstract base class, it is often not necessary to decide, if derived classes should be copyable. Slicing is not possible, because these classes cannot be instantiated. So in this case nothing has to be done.

Personal tools