Chapter 2 constructor/destructor/assignment function
Article 05: understand which functions are compiled and called in C ++
The N compiler can secretly create default constructor, copy constructor, copy assignment operator, and destructor for the class.
Copy constructor and copy assignment operator. The version created by the compiler simply copies each non-static member variable of the source object to the target object.
Clause 06: if you do not want to use a function automatically generated by the compiler, explicitly reject it.
N to reject the skills automatically provided by the compiler, the corresponding member functions can be declared as private and not implemented. Using base classes like uncopyable is also a practice.
Declare the copy constructor or the copy assignment operator as private. This prevents the compiler from creating its own version. However, the member function and the friend function can still call this private function.
Customize an uncopyable class:
Class uncopyable {
Protected:
Uncopyable (){}
~ Uncopyable (){}
PRIVATE:
Uncopyable (constuncopyable &);
Uncopyableoperator = (const uncopyable &);
}
The only thing that needs to be done is inherit uncopyable.
Class A: Private uncopyable {
....
};
When the compiler tries to generate a copy function, these functions attempt to call the corresponding brother of the base class, but the copy function of the base class is private and will be rejected by the compiler. This technology may lead to multiple inheritance.
Cla07: declare virtual destructor for a polymorphism base class
N Polymorphic (with polymorphism) base classes should declare a virtual destructor. If the class has any virtual function, it should have a virtual destructor.
N classes should not be declared if it is not used as a base classes or for Polymorphism purposes.
When a derived class object is deleted through a base class pointer and the base class carries a non-virtual destructor, the derived component of the object is not destroyed.
Any class with a virtual function is almost certainly expected to have a virtual destructor.
Cla08: do not escape exceptions from destructor
N destructor do not spit out exceptions. If a function called by the Destructor may throw an exception, the Destructor should capture any exceptions and swallow them or terminate the program.
N if the customer needs to respond to exceptions thrown during the running of an operation function, the class should provide a common function (not in the destructor) to execute this operation.
Cla09: virtual functions are never called during constructor and destructor.
N do not call virtual functions during constructor and destructor, because such calls never fall to derived class.
If the base class constructor calls the virtual function, the derived class has not yet been generated and cannot be downgraded to the derived class.
Clause 10: Make operator = return a reference to * This
N returns a reference to * this for the value assignment operator.
A & operator = (const &){
...
Return * this;
}
Clause 11: handle "self-assignment" in operator ="
Clause 12: Do not forget every component of an object when copying it.
The N copying function should assign a value of "all member variables in the object" and "All baseclass components"
N. Do not use a copying function to implement another copying function. The common function should be put into the third function and called by the two copying functions.