Why and when is a virtual destructor needed?

Answer Posted / uma sankar pradhan

A virtual destructor is needed when we are deleting a
object of derived class using a base class pointer.
i.e.,
base *b=new derived;
delete(b);
Let's say,we have allocated memory dynamically in derived
class constructor to a pointer data member and we
deallocated it in the destructor to avoid memory leakage
When the object is deleted through base class pointer,
only the base class destructor is invoked.consequently,the
dynamically allocated space remains unreleased.so it leads
to memory leak

Is This Answer Correct ?    34 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain virtual inheritance?

680


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1402


What is polymorphism in oops?

554


What is a function in oop?

624


What is solid in oops?

593






Is abstract thinking intelligence?

586


What is the types of inheritance?

594


Prepare me a program for the animation of train

1982


What is encapsulation example?

543


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1383


Where is pseudocode used?

559


What is the diamond problem in inheritance?

570


What is overloading in oop?

568


write string class as your own class in java without using any built-in function

1968


can inline function declare in private part of class?

3651