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


Please Help Members By Posting Answers For Below Questions

Ask to write virtual base class code?

2142


What is the keyword auto for?

561


Write a program to concatenate two strings.

577


What is switch case in c++ syntax?

615


an integer constant must have atleast one a) character b) digit c) decimal point

547






How to tokenize a string in c++?

595


Why is c++ called oops?

548


Why is it necessary to use a reference in the argument to the copy constructor?

623


Mention the ways in which parameterized can be invoked. Give an example of each.

561


What is the best c++ book for beginners?

564


What is friend class in c++ with example?

492


Can comments be nested?

618


What are files in c++?

583


What is basic if statement syntax?

557


Are c and c++ similar?

577