when can we use virtual destructor?
Answer Posted / 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 |
Post New Answer View All Answers
How many namespaces are there in c++?
What are virtual constructors/destructors?
write a programme to get a character and thier ASCII value
How should a contructor handle a failure?
What is abstract class in c++?
What is virtual destructor? What is its use?
What is lvalue?
What are the various access specifiers in c++?
When does the c++ compiler create temporary variables?
Can we distribute function templates and class templates in object libraries?
What does new return if there is insufficient memory to make your new object?
Difference between overloading vs. Overriding
What is the cout in c++?
What is stack unwinding?
What is the difference between ++ count and count ++?