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

Can I make ios apps with c++?

658


What are the differences between the function prototype and the function defi-nition?

707


Explain the static member function.

815


What does it mean to declare a member function as static?

690


What are the uses of pointers?

665






Please explain the reference variable in c++?

703


what are the events occur in intr activated on interrupt vector table

1265


In int main(int argc, char *argv[]) what is argv[0] a) The first argument passed into the program b) The program name c) You can't define main like that

709


What are the various storage classes in C++?

749


why is iostream::eof inside a loop condition considered wrong?

679


What is srand c++?

677


When must you use a pointer rather than a reference?

684


What are virtual constructors/destructors?

648


Give 10 points of differences between C & C++.

737


Can non graphic characters be used and processed in C++?

795