Part I: Constructors (Rules 11, 12, and 13)
Meyers gives three guidelines for constructors: define a copy constructor, use initialization rather than assignment, and list member s in order declared.
11. Define a copy constructor and an assignment operator for classes with dynamically allocated memory
For classes that use pointers, C++ may not know how to handle assignment and copying of a class. If you let C++ use its default assignment operator (=) in an assignment _or_ copy situation, the results may get messy. The solution to this problem is to write your own copy constructor and assignment operator.
12. Prefer initialization to assignment in constructors
When constructing an object, there are two options to passing parameters to the corresponding data members. One is to use the member initialization list. The other is to make assignments in the constructor body. For efficiency, prefer initialization except whn you have a large number of data members of built-in types.
13. List members in an initialization list in the order in which they are declared
Class members are initialized in the order of their declaration in the class. The order in which they are listed in a member initialization list makes no difference. Therefore, be sure to list members in initialization lists in the order they are declared in the class.
Part II: Destructors (Rule 14)
14. Make destructors virtual in base classes Making the destructor virtual will cause C++ to call the destructor in the class to which the pointer (or reference) really points. Once it has called the destructor for the derived class, it will continue to call the destructors all the way up the inheritance class. However, don't make destructors virtual if a class is not meant to be used as a base class (or if there isn't at least one virtual function). For abstract classes, a trick when one is without virtual functions is to declare a virtual destructor (but you need to provide a definition -- empty, if need be -- for it).
Part III: Assignment Operators(Rules 15, 16, 17)
Meyers gives three guidelines on the use of assignment operators: return a reference to *this, assign to all data members, and check for assignment to self.
15. Have operator= return a reference to *this Designing the assignment operator to return *this, returns the left-hand argument, which allows for chaining. It also allows you to declare operator='s parameter as a reference-to-constants.
16. Assign to all data membes in operator= When designing your own assignment operator, it is important to assign to all data members, including those of the base class. A trick in using this is, within the assignment operator, to make an explicit call to the base assignment operator.
17. Check for assignment to self in operator=
For efficiency's sake make sure you don't assign to self. In the best situation, this may waste time. In the worst, the data is destroyed and both operands are left with nothing. There is, however, the question of object identity, from value equality to address in memory.