virendra
September 4th, 1999, 04:55 AM
what is the significance of pure virtual destructor.i have read "we must provide the body to the pure virtual destructor".then how it is pure virtual?
|
Click to See Complete Forum and Search --> : pure virtual destructor virendra September 4th, 1999, 04:55 AM what is the significance of pure virtual destructor.i have read "we must provide the body to the pure virtual destructor".then how it is pure virtual? Rob Wainwright September 5th, 1999, 02:38 PM Pure virtual destructor is an estoric part of C++. I don't know of any reason to have one (unless your class must be an abstract base class but you haven't any sane functions to make pure virtual (so why not use the destructor). The reason you have to provide a destructor is something to do with either the C++ language or the destruction order (but I can't remember which). For more info, look at the C++ Faqs on the net. Rob, Sef September 5th, 1999, 05:06 PM The last post was right. Pure virtual destructors are one of those fuzzy areas without pulling out my copy of the standard to check its intended use. However, I've never seen such a function in real code. I'd be interested in knowing why you would need it though. The standard way is to simply define a virtual destructor (not pure) with an empty implementation. The reason you need it even though it does nothing is simple. Let's say you create a base class B without a virtual destructor and then derive a class D from B. Then somewhere in your code you have the following: D *pD = new D; Func(pD); void Func(B *pB) { delete pB; } Since no virtual destructor is defined for B, the destructor for D doesn't get called. Ooops, now your in trouble. So the solution is to simply define an empty virtual destructor for B. Problem solved. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |