Declare virtual destructor for the polymorphism base class

Source: Internet
Author: User

Declare virtual destructor for the polymorphism base class (declare Destructors virtual in polymophic base classes .)

If the base class has a non-virtual destructor, the result of deleting a derived class via the base class pointer is defined, which will cause partial destruction of the object: the base class part is destroyed, while the derived part is destroyed.

If you declare a virtual destructor for the class without reason, the object size will be increased. Some operations cannot be performed (mainly conversions involving internal details ).

Virtual destructor are declared only when the class contains at least one virtual function.

Do not integrate any classes without virtual destructor. C ++ does not provide a mechanism similar to Java to prohibit derivation.

Destructor are called at the deepest level, and then the destructor of base class is called.

A virtual destructor for base classes. This rule applies only to polymorphic (with polymorphism) base classes. The purpose is to process the derived classes object through the base classes interface.

Concurrent all base
Classes are used for Polymorphism purposes. Non-polymorphism uses base classes without virtual destructor.

Note:

Ø
Polyphonic (with polymorphism) base classes should declare a virtual destructor. If the class has any virtual function, it should have a virtual destructor.

Ø
Classes should not be declared if it is not used as a base class or for polymorphism (polyphonically.

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.