Item 5: Know what functions C++ silently writes and calls
* Compilers may implicitly generate a class's default constructor, copy constructor, copy assignment operation, and destructor.
Item 6: Explicitly disallow the use of compiler generated functions you do not want
* To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations. Using a base class like Uncopyable is one way to do this.
Item 7: Declare destructors virtual in polymorphic base classes
* Polymorphic base classes should declare virtual destructors. If a class has any virtual functions, it should have a virtual destructor.
* Classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.
Item 8: Prevent exceptions from leaving destructors
* Destructors should never emit exceptions. If functions called in a destructor may throw, the destructor should catch any exceptions, then swallow them or terminate the program.
* If class clients need to be able to react to exceptions thrown during an operation, the class should provide a regular (i.e., non-destructor) function that performs the operation.
Item 9: Never call virtual functions during construction or destruction
* Don't call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor.
Item 10: Have assignment operators return a reference to *this
* Have assignment operators return a reference to *this.
Item 11: Handle assignment to self in operator=
* Make sure operator= is well-behaved when an object is assigned to itself. Techniques include comparing addresses of source and target objects, careful statement ordering, and copy-and-swap.
* Make sure that any function operating on more than one object behaves correctly if two or more of the objects are the same.
Item 12: Copy all parts of an object
* Copying functions should be sure to copy all of an object's data members and all of its base class parts.
* Don't try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call.
阅读(277) | 评论(0) | 转发(0) |