is virtual in the destructor necessary?

Source: Internet
Author: User

We often hear the suggestion that the constructor cannot be virtual, and that the destructor is best for virtual, which is why?

Here's an example:

// pvtable1.cpp: Defines the entry point of the console application.

#include"stdafx.h"#include<iostream>using namespacestd;classBase1 { Public: Base1 () {cout<<"base1::base1 ()"<<Endl;} ~base1 () {cout <<"base1::~base1 ()"<<Endl;} Virtual voidF () {cout <<"Base1::f"<<Endl;} Virtual voidG () {cout <<"base1::g"<<Endl;} Virtual voidH () {cout <<"base1::h"<<Endl;}};classBase2 { Public: Base2 () {cout<<"Base2::base2 ()"<<Endl;} ~base2 () {cout <<"Base2::~base2 ()"<<Endl;} Virtual voidF () {cout <<"Base2::f"<<Endl;} Virtual voidG () {cout <<"base2::g"<<Endl;} Virtual voidH () {cout <<"base2::h"<<Endl;}};classBASE3 { Public: Base3 () {cout<<"base3::base3 ()"<<Endl;} ~base3 () {cout <<"base3::~base3 ()"<<Endl;} Virtual voidF () {cout <<"Base3::f"<<Endl;} Virtual voidG () {cout <<"base3::g"<<Endl;} Virtual voidH () {cout <<"base3::h"<<Endl;}};classDerive: PublicBASE1, PublicBase2, PublicBASE3 { Public: Derive () {cout<<"Derive::D erive ()"<<Endl;} ~derive () {cout <<"derive::~derive ()"<<Endl;} Virtual voidF () {cout <<"Derive::f"<<Endl;} Virtual voidG1 () {cout <<"DERIVE::G1"<<Endl;}};intdoIt2 () {Base1*pbase1 =NewDerive (); DeletepBase1; return 0;}int_tmain (intARGC, _tchar*argv[]) {doIt2 (); return 0;}

When using a multiple inheritance, when using the parent class's pointer to delete the subclass, it is found that can not be normal destructor, the result of the execution is as follows:

Normal if the destructor of the parent class is defined as a virtual function

It is always beneficial to set all member functions as virtual functions as possible in a class. Although it will increase overhead

The following are considerations for setting virtual functions:

1. Only member functions of a class can be declared as virtual functions.

2. A static member function cannot make a virtual function because it is not restricted to an object.

3. Inline functions cannot make virtual functions.

4. A constructor function cannot be a virtual function.

There are several situations where virtual is not possible:
1, as a non-public base class. Virtual destructor is not required for classes used only as private base class
2. The base class is not used as an interface.
3. If you can guarantee that this class is not inherited by public (private/protected inheritance, you cannot use a base pointer to a derived class in a non-friend function/Class)
4. If all of its derived classes (including derived classes of derived classes) are destructors of trivial (here trivial refers to what does not do at the programmer's level)
5. If you do not need a pointer to a base class to point to the object of the derived class
In these five cases, it is possible not to declare destructors as virtual, let alone be more efficient-but only if you have to make sure that the premises are established-but these assurances are often difficult to 100%: who can guarantee that when someone derives your class, the destructor is trivial, Or does someone else use a pointer to a base class that you provide to the derived class object? These are often hard to get guaranteed.

is virtual in the destructor necessary?

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.