Skip to main content

C++ use of Virtual Destructor

Destructors:




Destructors destroy object of its class type. After destructor body of some class Derv is executed, destructor for data members of class Derv are called, then destructor of direct base class of Derv, then virtual base class of Derv.

Virtual Destructor:

Why virtual destructor?

class Base
{
   ~Base(){}
};

class Derv:public Base
{
   ~Derv(){}
};

int main()
{
   Base *ptr = new Derv;
   delete ptr;
}


Consider the above example, where the destructor is not virtual. When the destructor get auto invoked for operation delete on base class pointer (ptr in our case) pointing to derived class (class Derv in our case) object, the compiler will statically bind the call to destructor of Base because compiler looks only at the type of the pointer(Base in this case) and not on type of its content( Derv in this case). So destructor of derived class Derv is never called.

To achieve the correct result as it would have been when you define a derived class object, you need to make the destructor to be virtual.

Making the destructor of Base virtual makes destructor of Derv also to be virtual.

Now the compiler will not bind destructor with operation delete of Base class pointer( ptr in our case) pointing to derived class object at compile time. It will be left and decided at run time depending content of the base class type pointer. So it calls destructor of Derv class. This in turn calls destructor of Base class.



Virtual constructor:

Constructors cannot be made virtual. As per the C++ standard,

A constructor shall not be virtual

GCC gives " error: constructors cannot be declared virtual ".

class A
{public:
virtual A(){}
virtual ~A(){}
};

The above code fails with compile error: constructors cannot be declared virtual


An Example:

#include<iostream>

using namespace std;

class base
{

public:
base()
{
cout<<"inside base const\n";
}

virtual ~base()  //declaring destructor of base as virtual to call child class destructor implicitly when we delete pointer to the base class pointing to the newly created instance of child1 class.
{

cout<<"inside base class destructor"<<endl;


}

};

class child1:public base
{
public:
child1()
{
cout<<"inside child conts\n";
}

~child1()
{

cout<<"inside child class destructor"<<endl;

}


};


int main()
{

base *bptr=new child1;
delete bptr;
return 0;
}

Comments

Popular posts from this blog

CDAC CCAT Rank - Which Centre you Should go for...

Subscribe us for our YouTube channel and any kind of help Click here to ask questions regarding CDAC 1. C-DAC (Head Quarters) Pune    CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC                         click below to know about the CCAT's This batch allotment                click above to know about the CCAT's This batch allotment Rank 1-300 c-dac HQ has been the best from the start. 2.  Sunbeam Pune Rank 300-500 I got very positive feedback from my friends who are in c-dac banglure main campus,since there are many companies you may get more opportunities. 3.  C-DAC Knowledge Park Rank 400-700 It as very good faculty .Almost all the students get placed here every year. 4. C-DAC Hyderabad Rank 200-1000 (It depends on the course which you select) C-DAC hyderabad is very good for the course PG-DESD. PG-DESD course is in hyderbad is better than pune HQ as per the past feedback.It as v

CDAC COURSES AND PLACEMENTS, WHICH IS BETTER FOR YOU

Subscribe us for our YouTube channel and any kind of help   Click here to ask questions regarding CDAC Before going through this post, I would like to draw your attention towards the importance of this post. This page not only explains my experience in CDAC but also aims at answering the queries of you all who are going or looking to have a course from CDAC. Kindly post your queries at the bottom of this page and we will get back to you within 24 hours. Kindly do not post your queries as an Anonymous user and do not forget to subscribe via email so as to keep track of your query.                         click below to know about the CCAT's This batch allotment                 click below to know about the CCAT's This batch allotment ______________________________________________ NOW a day lot of the graduates and post graduates are wondering most of the times on which course they should go for. What are the pros & cons of joining a particular ce

Placement Statistics - 2011 & 2012

Subscribe us for our YouTube channel and any kind of help                          Post your queries below and we will get back to you in no more than 24 hours.  Click here to ask questions regarding CDAC                         click below to know about the CCAT's This batch allotment                     click above to know about the CCAT's This batch allotment CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC Click here to see placement statistics About C-CAT, Exam Pattern and Books No. of Seats Across Various Training Centres Important Dates - 2014 Tags: CDAC, CDAC scope in future, CDAC placements, CDAC training, CDAC recruitment, CDAC training centres, DSSD, DESD, DAC, PGDSSD, PGDESD, PGDAC, DABC, PGDABC, VLSI, PGDITISS, PGDIVESD, PGDESD, PGDWiMC, placement statistics