How do you know that your class needs a virtual destructor?

Answer Posted / som shekhar

If we are deriving from the base class and the base class
consist of some virtual function , then in this case the
base destructor should be virtual.
if you do some thing like this :

base* b = new derived;
delete b;

In this case if base destructor is NOT VIRTUAL then only
base destructor is called and derived destructor wont be
called resulting into memory leak.

But if the base destructor is virtual then derived class
destructor and base destructor both will be called.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the methods of exporting a function from a dll?

652


What data structure is fastest, on average, for retrieving data: a) Binary Tree b) Hash Table c) Stack

593


What is the function to call to turn an ascii string into a long?

605


How can you say that a template is better than a base class?

587


Describe the setting up of my member functions to avoid overriding by the derived class?

614






What are proxy objects in c++?

645


What is input operator in c++?

591


How is new() different from malloc()?

636


What are manipulators in c++ with example?

590


What are arithmetic operators?

546


What is static in c++?

597


What is decltype c++?

559


What is the difference between structure and class?

564


Difference between pointer to constant and constant pointer to a constant. Give example.

653


Why are arrays usually processed with for loop?

775