|
-
September 4th, 1999, 04:55 AM
#1
pure virtual destructor
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?
-
September 5th, 1999, 02:38 PM
#2
Re: pure virtual destructor
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,
-
September 5th, 1999, 05:06 PM
#3
Re: pure virtual destructor
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|