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
1. destructor of derived.
and then
2. destructor of base.

but if base* bptr = new derived();

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
Base(){ cout<<"Constructor: Base"<<endl;}
~Base(){ cout<<"Destructor : Base"<<endl;}
class Derived: public Base
//Doing a lot of jobs by extending the functionality
Derived(){ cout<<"Constructor: Derived"<<endl;}
~Derived(){ cout<<"Destructor : Derived"<<endl;}
void main()
Base *Var = new Derived();
delete Var;

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.

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 do you know about near, far and huge pointer?

0 Answers  

write a program that a 5 digit number and calculates 2 power that number and prints it.

2 Answers   Vimukti Technologies,

Define a program that reads two matrices of size 3x3 with real values from the user then prints their sum, difference and multiplication.

0 Answers   TCS,

Must accepts "Maestro Cards" Tax for bike should be less than 15 Total number of lanes is more than 10 Must provides monthly pass Write a method: boolean isGoodTollBridge(String[] cardsAccepted, String[] tollTax, boolean hasMonthlyPass, int numberOfLanes); String[] cardsAccepted A String array of names of card types accepted for payment of toll tax, it can be null if the toll does not accept any card String[] tollTax A String array of toll tax chart (say “Train : 300”,”BullCart : 10”) boolean hasMonthlyPass This parameter defines whether there is any monthly pass available or not int numberOfLanes This parameter defines the number of lanes for each side

0 Answers   Telecom,

How would you differentiate between a pre and post increment operators while overloading?

0 Answers  

What is c strings syntax?

0 Answers  

What is a modifier in c++?

0 Answers  

Is eclipse good for c++?

0 Answers  

When should you use global variables?

0 Answers  

What is the benefit of learning c++?

0 Answers  

What does it mean to declare a member variable as static?

0 Answers  

Is C++ case sensitive a) False b) Depends on implementation c) True

0 Answers