Thread View: comp.std.c++
2 messages
2 total messages
Started by Chris Parker
Fri, 10 Nov 1995 01:08
Re: Virtual functions and destructors
Author: Chris Parker
Date: Fri, 10 Nov 1995 01:08
Date: Fri, 10 Nov 1995 01:08
58 lines
3849 bytes
3849 bytes
> > Is it acceptable according to the standard for the destructor to be the onl
y
> > virtual member in the class?
>
> Sure, but it's kind of pointless. You only need a virtual destructor
> when you will be handling objects of a derived class through a pointer or
> reference whose static type is some base class, and in that case you will
> (nearly?) always need other virtual functions to provide the polymorphic
> interface that you'll use. The only exception I can imagine offhand
> would be if you were doing nothing but holding a bunch of them, as in a
> collection of some sort; but how could you ever do anything to the
> collected objects without virtual functions? Seems to me that the only
> thing you could do if they have no virtual function except the destructor
> is delete them, and if that's all you're doing... well, I suppose there
> could be some reason for deferring deletion for a bunch of mixed
> objects. It just doesn't seem real likely to me that that woudl be the
> ONLY action you'd want to take on them in a polymorphic fashion.
>
> But it's certainly legal to have only the destructor virtual.
You can do polymorphic things without any virtual functions. Casting a child
to a parent which has no virtual functions makes the class behave like it was
a class of that parent. You might want to delete it while it has been the
parent in a polymorphic sense. Thus, the destructor is the only virtual
function.
> > Is virtualness inherited by destructors? Does C++ automatically make all
> > applicable superclasses also have virtual destructor if they don't already?
>
> Is there any inheritance of anything from subclass to superclass? Or did
> you mean to write "subclass" there? Inheritance goes in only one
> direction: a class derived from a base class with a virtual destructor
> will have a virtual destructor. Deriving a class whose destructor is
> declared virtual from a base class with a non-virtual destructor doesn't
> magically change the base class in this respect (nor any other).
I did mean superclass when I said it. Based on what I've learned by thinking
about these things, I'm pretty convinced that all destructors should be
virtual all the time (implied). I can't find a single case where it would
make sense to destroy an object and not have all proper cleanup happen.
> dispatch. No magic, an object's actual type cannot affect anything done
> to it through a pointer or reference of a base type unless the operation
> is through a function that is virtual in that base type.
The thing that doesn't work for me is that the pointer type is
different, but it still has the same pointer value. At the low-level, you
are freeing the same block of memory either way. In order to
call a destructor, the system needs to convert the pointer to a class type
instance anyway.
Chris Parker
parker@cs.wpi.edu
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Virtual functions and destructors
Author: Matt Austern
Date: Fri, 10 Nov 1995 19:58
Date: Fri, 10 Nov 1995 19:58
32 lines
2181 bytes
2181 bytes
In article <199511091835.NAA11661@starling.WPI.EDU> Chris Parker <parker@owl.WPI.EDU> writes: > I did mean superclass when I said it. Based on what I've learned by thinking > about these things, I'm pretty convinced that all destructors should be > virtual all the time (implied). I can't find a single case where it would > make sense to destroy an object and not have all proper cleanup happen. This falls into the category of "if you don't use it, you shouldn't pay for it." A class that has virtual functions is more expensive than a class that doesn't have any; in some cases that overhead can be prohibitive. So that's the case in which you want a non-virtual destructor: if the extra overhead of virtual functions would be a problem, and if you don't expect your class to be used as a base for inheritance, then you shouldn't make the destructor virtual. This is reasonably common; consider, for example, complex<double>. That's why the standard allows non-virtual destructors. It probably is true that if you have any virtual functions at all then you should have a virtual destructor. Most compilers do issue warnings if you have a polymorphic class without a virtual destructor. -- Matt Austern He showed his lower teeth. "We matt@physics.berkeley.edu all have flaws," he said, "and http://dogbert.lbl.gov/~matt mine is being wicked." --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads