I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....
Bye...Take Care...Give Reply
virtual destructer is used so that while deleting the pointer to base class but pointing to base class invokes the derived class destructor first,then the base class destruct hence preventing memory leak...
First pls get clear with virtual function concepts...
suppose we have a class hierachy
Empl-->Manager-->CEO
now we are creating
Empl* e1;
CEO c1;
e1= & c1;
Now when ever my e1 goes out of scope because e1 is of type Empl ,only the destructor of e1 is called & memory is freed for attributes of e1 only. so we have the memory leakage problem
so if we are making Destructor as a virtual so exceutin of destructor will follow
~CEO-->~Manager-->~Empl path so Memory for all the data is freed.....
so to avoid these Memory Leakage problem we have to make destructor as a virtual destructor.....