Question:
What Is The Use of VirtualDestructer?
Answer:
Virtual destructor is used for the polymorphic decomposition of allocated resources. Source: CoolInterview.com
Answered by: Vishal Singh | Date: 9/14/2007 9:50:40 PM
| Contact Vishal Singh
in the case of multiple inheritance same member function in both and derived class exist then the two copies of childs will be created. so there is an ambiguity between which function to execute so virtual functions are introduced.we can accept the derived and based class methods with help of pointers.if pointer points to base method then base method will be executed.if it points to derive then derive will be executed so in order to delete these pointers we go for virtual desstructors Source: CoolInterview.com
Answered by: krishna | Date: 9/16/2007 10:54:24 AM
| Contact krishna
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... Source: CoolInterview.com
Answered by: praveen | Date: 9/22/2007
| Contact praveen
First pls get clear with virtual function conceptss... 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. ¬ for Manager & CEO attributes 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.....
Now if u have understood the explanation pls tell me Can we have virtual constructor???
Source: CoolInterview.com
Answered by: Mihir P Trivedi | Date: 9/23/2007
| Contact Mihir P Trivedi
virtual destructors are used to deallocate the memory in prper order if the memory is allocated through new for example base *p; p=new derive; firstly the base class constructor would be called followd by the derive class if we now say delete p then destruction should proceed from derive to base. this can be ensured by declaring base class destructor virual Source: CoolInterview.com
Answered by: Rohit Adhlakha | Date: 9/23/2007
| Contact Rohit Adhlakha
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|