when can we use virtual destructor?

Answers were Sorted based on User's Feedback



when can we use virtual destructor?..

Answer / richa

It is used whenever a base class pointer is pointing to its
derived class.In such a case when a user tries to delete the
base class pointer then it results in deallocating the
memory occupied by the base class.Therefore instead the
derived class getting destroyed the base class does.Now as
the base class gets destroyed the base class pointer which
was pointing to its derived class hold no meaning as it is
already destroyed.
n such a case we should make the destructors of the base
class virtual so that whenever a delete is called on the
base class pointer then as the destructor is virtual the
compiler will call the destructor of the respective derived
class.Hence the scenario wont be breached when a base class
pointe points to derived class as it would help deleting the
respective derived class object.

Is This Answer Correct ?    86 Yes 5 No

when can we use virtual destructor?..

Answer / n

Virtual Destructor is a concept, comes into picture when one
will try to delete the base object pointer pointing to
derived class.
Base* pb = new Derived();
delete pb;
In this case if Base class destructor is not virtual then
only base class destructor will be called up for clean up.

While if we make Base class destructor as virtual then
1. Derived class destructor will be called
2. Base class destructor will be called up

Proper clean up of the objects from derived as well as base
class. Mission Accomplish

Is This Answer Correct ?    36 Yes 1 No

when can we use virtual destructor?..

Answer / sagarson

Need for a virtual destructor
1.destructor for the base parts are invoked automatically
2.we might delete a ptr to the base type that actually
points to a derived object
3.if we delete a ptr to base then the base class destructor
is run and the members of the base class are cleared up. If
the object is a derived type then the behavior is undefined
4.to ensure that the proper destructor is run the destructor
must be virtual in the base class
5.virtual destructor needed if base pointer that points to a
derived object is ever deleted (even if it doesnt do any work)

Is This Answer Correct ?    34 Yes 4 No

when can we use virtual destructor?..

Answer / achal ubbott

We should know the proper sequence of calling of
destructors.
1. destructor of derived.
and then
2. destructor of base.

but if base* bptr = new derived();

then
delete bptr;

can behave wrong and violate the sequence. got it?

Is This Answer Correct ?    15 Yes 0 No

when can we use virtual destructor?..

Answer / pradeep

This example fully describe the need of Virtual Destructor
in base class:-
----------------------------------------------------------
#include <iostream.h>
#include <stdio.h>
class Base
{
public:
Base(){ cout<<"Constructor: Base"<<endl;}
~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
};
void main()
{
Base *Var = new Derived();
delete Var;
getch();
}

-----------------------------------------------------------
When it will be executed..it will show only that Base Class
destructor executed not the Derived.
But if we make Base class destructor "virtual"
(i.e. virtual ~Base(){ cout<<"Destructor : Base"<<endl;} )
then we can verify that Destructor execute into this order:--
1. Derived class destructor
2. Base class destructor

---If there is any mistake kindly let me know.
Thanks...!!!

Is This Answer Correct ?    13 Yes 1 No

when can we use virtual destructor?..

Answer / neeraj tyagi

I agreed with Pradeep's answer:)

Is This Answer Correct ?    4 Yes 2 No

when can we use virtual destructor?..

Answer / yaggu

Basically, the characteristics it self defines, that
constructor can not be virtual but overloaded while in the
inverse to this, the destructor can not be
overloaded....means there may be more than one constructors
in class and when you create the object of the class,
constructor invokes and when you do not need at the same
time though you define more than one constructors but with
only one destructor we can destroy the object.....that is why...

Is This Answer Correct ?    4 Yes 4 No

Post New Answer

More C++ General Interview Questions

What are c++ stream classes?

0 Answers  


Why would you make a destructor virtual?

3 Answers   Lehman Brothers,


what are the events occur in intr activated on interrupt vector table

0 Answers   HGS,


What are activex and ole?

0 Answers  


How long does it take to get good at leetcode?

0 Answers  






What is pure virtual function?

0 Answers  


Is c++ a difficult language?

0 Answers  


Ask to write virtual base class code?

0 Answers   Satyam,


How to demonstrate the use of a variable?

0 Answers  


What is the cout in c++?

0 Answers  


When the design recommends static functions?

2 Answers   Symphony,


What is array give example?

0 Answers  


Categories