Effective C + + study notes clause 8-10

Source: Internet
Author: User

Article 7: Declaring virtual destructors for polymorphic base classes

1. When a base class pointer accepts the address of a derived class object, the pointer is delete, releasing only the base class part

2, to all classes with the virtual destructor is a bad idea, will have vptr point to a function pointer array, expand the unnecessary object size, unless the compensation vptr, otherwise there is no portability.

3, the String class and the STL does not contain the virtual destructor, however some users use them as the base class, using Delete to point to the base class pointer of the derived class, causing the error "c++11 added forbidden derivation nature", they are not suitable when the base class.

4, there is no suitable pure virtual function at hand, but you do need an abstract class, it is good to declare the destructor as pure virtual function, including the problem of virtual destruction, but you should remember to give him a definition, because it will be used when a destructor is derived from the class.

5. Virtual destruction is designed to handle derived class objects through a base class interface

6, some classes are to do the base class but not to support polymorphism.

7. Classes with polymorphic properties or classes with virtual functions should have virtual destructors, not for polymorphic categories with virtual destructors.

Article 8: Don't let an exception escape destructor "Review exception"

1, do not let the imaginary function throws an exception, destructors to catch any exceptions, swallow or end the program.

2. If the customer needs to react to an exception thrown during an operation, then CLSS should provide a normal function (rather than a destructor) to perform the operation

3. For resource management classes, set up a resource management class to manage objects as members, and provide functions for managing objects in the new class.

Article 9: Never invoke the virtual function "review" in the construction and destructor

1. In summary, do not use virtual functions in destructors and constructors, because the hierarchy of objects is different from the hierarchy of calls.

2. Use a static function to call the formal parameter to pass the data to the base class.

Effective C + + study notes clause 8-10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.