when can we use virtual destructor?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 ? | 14 Yes | 1 No |
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 |
What is anonymous object in c++?
What is the Difference between "vector" and "array"?
15 Answers Covansys, Gambit, TCS, Wipro,
How does class accomplish data hiding in c++?
What is the output of printf("%d")?
58 Answers CTS, HCL, Infosys, TCS, Winit, Wipro,
Why preincrement operator is faster than postincrement?
How do you allocate and deallocate memory in C++?
Is java as fast as c++?
What do you mean by persistent and non persistent objects?
WHAT IS THE ABREVATION OF FORTRAN?
What is the difference between Char a[ ]=”string” and char *a=”String”
Explain stack & heap objects?
Explain about Garbage Collector?