FAQ 30.13 Are there cases where one or two of the Big Three may be needed but not all three?

Yes, but define them all anyway.

There are cases where one or two of the Big Three may be needed but not all three. All three should usually be defined anyway so that people don't have to think so hard during code reviews and maintenance activities. Here are four common times when this happens: virtual destructors, protected: assignment operators, recording creation or destruction, and unnecessary or illogical copy operations.

Virtual destructors: A base class often has a virtual destructor to ensure that the right destructor is called during delete basePointer (see FAQ 21.05). If this explicit destructor exists solely to be made virtual (for example, if it does what the synthesized destructor would have done, namely { }), the class may not need an explicit copy constructor or assignment operator.

Protected assignment operators: An ABC often has a protected: assignment operator to prevent users from performing assignment using a reference to an ABC (see FAQ 24.05). If this explicit assignment operator exists solely to be made protected: (for example, if it does what the synthesized assignment operator would have done, namely memberwise assignment), the class may not need an explicit copy constructor or destructor.

Recording creation or destruction: A class sometimes has an explicit destructor and copy constructor solely to record the birth and death of its objects. For example, the class might print a message to a log file or count the number of existing objects. If the explicit destructor or copy constructor exists solely to perform this information recording (for example, if these operations do what the synthesized versions would have done), the class may not need an explicit assignment operator, since assignment doesn't change the number of instances of a class.

Unnecessary or illogical copy operations: There are cases where a class simply doesn't need one or both copy operations. Sometimes the copy operations don't even make logical sense. For example, the semantics of class File may mean that it is nonsensical to copy File objects; similarly for objects of class Semaphore. In these cases, the unnecessary copy operations are normally declared in the private: section of the class and are never defined. This prevents the compiler from synthesizing these operations in the class's public: section and causes compile-time error messages whenever a user accidentally calls one of these member functions. In this case, it is not strictly necessary to define the other members of the Big Three just because one or both copy operations are declared in the private: section of the class.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net