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

Is c++ pass by reference or value?

564


Is c++ the most powerful language?

558


What is lambda in c++?

573


Write about an iterator class?

606


What is the difference between a "copy constructor" and an "assignment operator" in C++?

612






Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

2432


Mention the storage classes in c++.

636


Give example of a pure virtual function in c++?

581


Explain the concept of dynamic allocation of memory?

612


What are function prototypes?

640


What's c++ used for?

587


What is java and c++?

656


How is computer programming useful in real life?

589


How would you differentiate between a pre and post increment operators while overloading?

644


Write syntax to define friend functions in C++.

597