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 ? | 13 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 |
Is c++ a high level language?
What is ifstream c++?
Difference between static global and global?
16 Answers Microsoft, Symphony, Wipro,
Specify some guidelines that should be followed while overloading operators?
what is the behaviour of C and C++ compiler for the below statements. int *p; p = malloc(100); Is the behaviour same ? or different ?
what is the size of this class class size { public: char data1; double d; int data2; char data3; double data4; short data5; }; please explain the padding for these double variables.
How much do coding jobs pay?
Define a nested class. Explain how it can be useful.
Explain the difference between new() and malloc() in c++?
What is a volatile variable in c++?
Would you rather wait for quicksort, linear search, or bubble sort on a 200000 element array? (Or go to lunch...) a) Quicksort b) Linear Search c) Bubble Sort
What is new in c++?