Difference between revisions of "Cpp Coding Standards/CLSINIT/ObvCopy"

From Apache OpenOffice Wiki
Jump to: navigation, search
(inital version of details)
 
m (restructuring)
Line 1: Line 1:
 
===  Obvious Copyability ===
 
===  Obvious Copyability ===
[[../../CLSINIT#ObvCopy|Summary]]
+
==== [[../../CLSINIT#ObvCopy|Summary]] ====
 +
Make it obvious, if the class is intended to be copied. Forbid copying otherwise.
  
When designing a class, make clear the intention regarding copies and assignment. There are four possibilities:
+
==== Explanation ====
 +
 
 +
There are three possibilities:
  
 
# 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.
 
# 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.
 
# 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=().
 
# 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=().
# The class is not intended to be copied. Make this explicit by either deriving from [http://www.boost.org/libs/utility/utility.htm#Class_noncopyable boost::noncopyable] or declaring (but not implementing) copy constructor and operator=() private.
+
# The class is not intended to be copied. Make this explicit by  
# 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.
+
#* either deriving from [http://www.boost.org/libs/utility/utility.htm#Class_noncopyable boost::noncopyable]  
 +
#* or declaring (but not implementing) copy constructor and operator=() private.
 +
 
 +
==== Exceptions ====
 +
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.

Revision as of 12:01, 2 February 2007

Obvious Copyability

Summary

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

Explanation

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.

Exceptions

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