Pure Virtual Destructor
I noticed that you can make destructors pure - I was just wondering if there was ever a reason to do this? I assume that if it can be done, its there for a reason, but I don''t what it is
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
They''re useful when you''re manipulating a derived class via a base class pointer.
Let''s assume you''ve got 2 classes, Base which is the base class and Derived which is derived from base.
Without a virtual destructor, the following code will cause some problems.
Virtual destructors do for classes what virtual functions do. They allow the right function/destructor to be called.
Hope that helped. I read about virtual destructors from Bruce Eckels Thinking in C++ vol 1 (page 700).
Let''s assume you''ve got 2 classes, Base which is the base class and Derived which is derived from base.
Without a virtual destructor, the following code will cause some problems.
Base *bp = new Derived;//this is legal//do your stuff here.delete bp;//this calls the destructor for class Base, not Derived.
Virtual destructors do for classes what virtual functions do. They allow the right function/destructor to be called.
Hope that helped. I read about virtual destructors from Bruce Eckels Thinking in C++ vol 1 (page 700).
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Aye I know I needed a virtual destructor becuase I was suiciding an object in a parent class method (release).
But why would you make it pure?
Edited by - Magmai Kai Holmlor on February 20, 2001 10:44:59 AM
But why would you make it pure?
Edited by - Magmai Kai Holmlor on February 20, 2001 10:44:59 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Oh shoot. I thought I had one up on Magmai there Anyway, for the record I''ve never heard of a pure virtual destructor. And I really can''t think of any reason/case where I''d like a pure virtual destructor.
I''m not much help, am I?
I''m not much help, am I?
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
February 20, 2001 10:50 AM
There is more to it than this, but here is one example..
class Base
{
public:
Base(){ pObj = new Object; }
virtual ~Base() = 0;
private:
Object * pObj;
}
Base::~Base()
{
delete pObj;
}
you might have a base class with no methods and you absolutely don''t want people making Base objects. Notice you can still provide an impletemtation for a pure virtual function. A derived class will then be able to clean up properly.
class Base
{
public:
Base(){ pObj = new Object; }
virtual ~Base() = 0;
private:
Object * pObj;
}
Base::~Base()
{
delete pObj;
}
you might have a base class with no methods and you absolutely don''t want people making Base objects. Notice you can still provide an impletemtation for a pure virtual function. A derived class will then be able to clean up properly.
February 20, 2001 11:05 AM
CGameProgrammer:
There will be only one object created. The memory allocation is not done in the constructors. So the Base ctor and the Derived ctor are called given the same memory.
Knowing this you can, f.e. initialize a member from your base class again in your derived ctor, even if the ctor of your base class already initialized it.
Personal note:
dtors should always be virtual if you plan to derive classes. Its also nec. if you work in a dll environment with different allocation models. But ''pure'' virtual dtors sounds kind of kinky to me
There will be only one object created. The memory allocation is not done in the constructors. So the Base ctor and the Derived ctor are called given the same memory.
Knowing this you can, f.e. initialize a member from your base class again in your derived ctor, even if the ctor of your base class already initialized it.
Personal note:
dtors should always be virtual if you plan to derive classes. Its also nec. if you work in a dll environment with different allocation models. But ''pure'' virtual dtors sounds kind of kinky to me
CGameProgrammer:
There will be only one object created. The memory allocation is not done in the constructors. So the Base ctor and the Derived ctor are called given the same memory.
Knowing this you can, f.e. initialize a member from your base class again in your derived ctor, even if the ctor of your base class already initialized it.
Personal note:
dtors should always be virtual if you plan to derive classes. Its also nec. if you work in a dll environment with different allocation models. But ''pure'' virtual dtors sounds kind of kinky to me
There will be only one object created. The memory allocation is not done in the constructors. So the Base ctor and the Derived ctor are called given the same memory.
Knowing this you can, f.e. initialize a member from your base class again in your derived ctor, even if the ctor of your base class already initialized it.
Personal note:
dtors should always be virtual if you plan to derive classes. Its also nec. if you work in a dll environment with different allocation models. But ''pure'' virtual dtors sounds kind of kinky to me
The only reason I can think of to allow a destructor to be pure is for a handle class where the only method you have is the destructor, but you don''t want it instantiated. Does that make any sense? That is a really weird thing you''ve stumbled upon, Magmai.
It also might have something to do with the fact that a pure virtual function can have a definition (something I didn''t know until recently). Such a function could only be called explicitly by PureClas::function (). There are some memory management schemes that explicitly call destructors, so maybe that has something to do with it?
You know, another way to look at this might be: why wouldn''t you allow the destructor to be pure? When would it cause problems? Maybe it doesn''t, and that''s the key.
I''m looking through the ARM and there''s no mention of pure virtual in the destructor section. The only thing that might be relevant is the fact that destructors are not inherited, but I''m not sure what, if anything, that has to do with it.
It also might have something to do with the fact that a pure virtual function can have a definition (something I didn''t know until recently). Such a function could only be called explicitly by PureClas::function (). There are some memory management schemes that explicitly call destructors, so maybe that has something to do with it?
You know, another way to look at this might be: why wouldn''t you allow the destructor to be pure? When would it cause problems? Maybe it doesn''t, and that''s the key.
I''m looking through the ARM and there''s no mention of pure virtual in the destructor section. The only thing that might be relevant is the fact that destructors are not inherited, but I''m not sure what, if anything, that has to do with it.
This discussion is getting too abstract. The only logical reason for pure virtual functions is to force derived classes to override operations for which there is no correct "generic" operation.
That is obviously how virtual destructors work, by overriding the base class''s destructor. A derived object is not a new, singularly complete object in and of itself. A derived object is composed of numerous subobjects, which are instances of the base class(es).
It is important to remember that the virtual keyword has the exactsame meaning as with virtual functions, but that several destructors are called because there are several objects in an instance of a derived class.
It would be incorrect to say that several destructors are called on the same object.
I know you understand how this works, but the virtual destructor thingy is commonly misunderstood.
That is obviously how virtual destructors work, by overriding the base class''s destructor. A derived object is not a new, singularly complete object in and of itself. A derived object is composed of numerous subobjects, which are instances of the base class(es).
It is important to remember that the virtual keyword has the exactsame meaning as with virtual functions, but that several destructors are called because there are several objects in an instance of a derived class.
It would be incorrect to say that several destructors are called on the same object.
I know you understand how this works, but the virtual destructor thingy is commonly misunderstood.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement