V. Clause 05-Understand what functions C + + silently writes and calls
1.class, when C + + has processed it, the compiler declares a default constructor, a copy constructor, a copy assignment operator, and a destructor if the function is not declared by itself. Only when these functions are needed (called) will they be created by the compiler. The compiler-generated destructor is a non-virtual, unless the class's base class itself declares a virtual destructor
2. If class contains reference members or const members, and the copy assignment operator of base class is declared private, the compiler rejects the copy assignment operator for class
3.class destructors, whether compiler-generated or user-defined, automatically invoke destructors for their non-static member variables
VI. clause 06-If you do not use the compiled automatically generated function, you should explicitly reject
1. Declare functions that are not automatically generated by using compilation as private and deliberately do not implement them, so that when the client attempts to copy the class object, the compiler will block him. If you inadvertently do so within the member function or the friend function, it will be your turn to send a complaint to the connector.
2. It is possible to move the connector error to the compile date (and that is good, the sooner you detect the error the better), as long as the copy constructor and copy assignment operator are declared private, but not in class itself, But in a base class designed specifically to prevent copying actions. This base class is very simple:
Class Uncopyable
{
Protected
Uncopyable () {}
~uncopyable () {}
Private
Uncopyable (const uncopyable&);
uncopyable& operator= (const uncopyable&);
};
The rest is inherited from uncopyable with class:
Class Xxx:private Uncopyable
{
};
This makes sense, because when you try to copy an object, the compiler tries to generate a copy constructor and a copy assignment operator, and the compiler rejects the build.
VII. clause 07-Declaring a virtual destructor for a polymorphic base class
1. If class does not contain a virtual function, it usually means that it is not intended to be used as a base class. When class does not attempt to be treated as a base class, or if some class is designed to not have polymorphism, it is often a bad idea to make the destructor virtual.
2.polymorphic (with polymorphic) base class should declare a virtual destructor. If class comes with any virtual function, it should have a virtual destructor
VIII. clause 08-Don't let the anomaly escape the destructor
1. Destructors absolutely do not spit out exceptions. If a function called by a destructor might throw an exception, the destructor should catch any exceptions and swallow them (not propagate) or close the program
2. If the customer needs to react to an exception thrown during the operation of an action function, class should provide a normal function (rather than in the destructor) to do the action
clause 09-Never call the virtual function during the construction and destructor
1. During base class construction, the virtual function is not a virtual function
The type of the object is base class rather than derived class during the base class construction of the 2.derived class object. Not only the virtual function is parsed by the compiler to base class, and if you use Run-time type information (runtime type infomation, such as dynamic_cast and type_id), the object is also treated as a base class type. The same principle applies to destructors
3. Since you cannot use the virtual function to call down from base class, during construction, you can make up for it by "derived class to pass the necessary construction information up to the base class constructor"
10, Clause 10-make operator= return a reference to *this
1. In order to achieve "chain assignment", the assignment operator must return a reference to the left argument of the operator
2. This protocol also applies to all assignment-related operations, such as + =,-=, *=, etc.
11. Clause 11-handling "self assignment" in operator=
1. The so-called "alias" means "there is more than one method of referring to (referring to) an object"
2. Ensure that operator= have good behavior when the object is assigned to itself. Techniques include comparing the address of "source object" and "target object", thoughtful statement order, and Copy-and-swap technology
3. Determine that any function that operates more than one object, and where multiple objects are the same object, is still behaving correctly
12, Clause 12-do not forget each component when you copy the object
1. Any time you assume a major responsibility for writing the copying function for derived class, you must also copy its base class composition carefully. Those ingredients are often private, so you can't access them directly, you should let derived class's copying function call the corresponding base class function
The 2.copying function should ensure that "all member variables within the object" and "all base class elements" are copied
3. Do not attempt to implement another copying function with a copying function. The common function should be placed in the third function and called by two copying functions.