Topic: Pure Virtual Destructor Query
Author: het@despam.pangea.ca (Harvey Taylor)
Date: 1998/04/10 Raw View
Greetings,
A colleague mentioned to me that she always used
pure virtual destructors, which surprised me because I
don't see the intention behind them.
First, is the FDIS identical to the CD2 in section 12.4.7
where it says: "A destructor can be declared virtual (10.3)
or pure virtual (10.4);" ?
It is my understanding that with a pure virtual function
one implicitly says you must over-ride this function in a
derived class.
What can this possibly mean in the case of a pure
virtual destructor? In the example below, one cannot
put a BaseClass destructor in DerivedClass.
class BaseClass
{
public:
BaseClass (void);
virtual ~BaseClass (void) = 0;
};
class DerivedClass : public BaseClass
{
public:
DerivedClass (void);
virtual ~DerivedClass(void);
virtual ~BaseClass (void); // error
};
Is the pure virtual destructor simply to avoid having to
code a trivial destructor?
ie. virtual ~BaseClass (void) { };
Or is there something else going on here?
<curious>
-het
"Please understand I never had a secret chart, to get me to the heart,
of this, or any other matter." -Cohen
Harvey Taylor Internet: het@despam.pangea.ca
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/04/10 Raw View
Harvey Taylor wrote:
>
> Greetings,
> A colleague mentioned to me that she always used
> pure virtual destructors, which surprised me because I
> don't see the intention behind them.
>
> First, is the FDIS identical to the CD2 in section 12.4.7
> where it says: "A destructor can be declared virtual (10.3)
> or pure virtual (10.4);" ?
>
> It is my understanding that with a pure virtual function
> one implicitly says you must over-ride this function in a
> derived class.
>
> What can this possibly mean in the case of a pure
> virtual destructor?
Having a pure virtual destructor means two things:
1. You cannot create objects of the base type. This is two-edged; while
it supports Meyers's guideline "do not derive from concrete types,"
there are times when that really is what you want to do.
2. Derived classes must override the virtual function, or you will not
be able to create objects of the derived type.
When would you want to do this? Well, you should make the destructor
virtual in many abstract base classes, and you need to do it if it's the
only virtual function. You should make the destructor virtual if all
derived classes will need to do something special in the destructor;
that way a user of the base class can't screw up and inherit the
destructor. You should make the destructor virtual if not all derived
classes will need to override it, but NOT overriding it needs to be a
conscious decision.
But ALWAYS making a destructor pure virtual is a bad idea. It's like
NEVER using multiple inheritance or ALWAYS using the pimple idiom or
NEVER inlining before optimization.
I think there's even a Guru of the Week (from clc++.mod) that deals with
this subject. Look up GotW in DejaNews or find a link to the GotW
archives at my website (address below; on the links page).
--
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
My reply address is correct as-is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: het@despam.pangea.ca (Harvey Taylor)
Date: 1998/04/11 Raw View
In message <352E964A.98009A07@concentric.net>,
<bradds@concentric.net> (Bradd W. Szonye) writes:
|>
|> What can this possibly mean in the case of a pure
|> virtual destructor?
|
|Having a pure virtual destructor means two things:
|1. You cannot create objects of the base type. [...]
|
|2. Derived classes must override the virtual function, [...]
|
Then the question to my mind is what precisely does the phrase
"must override the virtual function" mean in the case of a
destructor?
In my testing (with MSVC), I see:
a) The derived class may not have a base class destructor.
b) The derived class does not need to have a destructor.
c) The pure virtual destructor in the base class must be defined.
Am I being mislead by a pre-standard compiler?
It seems to me the notion of over-riding a destructor is
questionable. Maybe I am being overly concrete here, but when
I over-ride a function I expect to see the same function name &
the same parameters in the two classes.
Illumination would be appreciated.
| [Discussion of virtual destructors elided.]
|
|I think there's even a Guru of the Week (from clc++.mod) that deals with
|this subject.
|
Thanks for the pointer.
<cordially>
-het
"Please understand I never had a secret chart, to get me to the heart,
of this, or any other matter." -Cohen
Harvey Taylor Internet: het@despam.pangea.ca
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/04/12 Raw View
In article gir@engnews1.Eng.Sun.COM, het@despam.pangea.ca (Harvey Taylor) writes:
>Greetings,
> A colleague mentioned to me that she always used
> pure virtual destructors, which surprised me because I
> don't see the intention behind them.
>
> First, is the FDIS identical to the CD2 in section 12.4.7
> where it says: "A destructor can be declared virtual (10.3)
> or pure virtual (10.4);" ?
>
> It is my understanding that with a pure virtual function
> one implicitly says you must over-ride this function in a
> derived class.
Paragraph 7 of 12.4 is identical in CD2 and the draft standard, and
the rule I think is exactly the same as in the ARM.
If you declare the destructor pure virtual, you cannot create
instances of the type, because the class by definition is an
abstract class. Of course, the same is true if the class has or
inherits ANY pure virtual functions. The destructor is not special
in that regard. Destructors can't be overridden, so even if the
base class has a pure virtual destructor, you don't necessarily have
to declare one in the derived class. The compiler will generate one
for you, assuming accessibility rules allow it.
In the unusual case where you want an abstract class but don't
have any virtual functions to declare in it, you could declare the
destructor pure virtual. The destructor of any class that you intend
to derive from should ordinarily be virtual, and making it pure
virtual is harmless even if not necessary. Of course, you must
provide a body for any declared destructor whether or not it is
pure virtual. (Any pure vitual function may have a body, and if it
does, it is ok to call it.)
I don't see any advantage in an abitrary rule to make all destructors
pure virtual.
---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/04/12 Raw View
> In message <352E964A.98009A07@concentric.net>,
> <bradds@concentric.net> (Bradd W. Szonye) writes:
> |Having a pure virtual destructor means two things:
> |1. You cannot create objects of the base type. [...]
> |
> |2. Derived classes must override the virtual function, [...]
Harvey Taylor wrote:
> Then the question to my mind is what precisely does the phrase
> "must override the virtual function" mean in the case of a
> destructor?
Actually, I may be wrong about this. See below.
> In my testing (with MSVC), I see:
> a) The derived class may not have a base class destructor.
What do you mean by "base class destructor"? Every class has exactly one
destructor: either the default destructor (that does memberwise
destruction) or a user-defined destructor (that does user stuff, then
memberwise destruction of base classes). I'm not familiar with the
concept of "base class destructor."
If you mean declaring "derived::~base()" -- you can't do that in any
class, pure virtual destructor or not.
> b) The derived class does not need to have a destructor.
This may be true; the default construct provided by the language most
likely overrides the pure virtual one anyway. Sorry for the misleading
information if I'm wrong.
> c) The pure virtual destructor in the base class must be
defined.
This is true. Destructors must always be defined, pure virtual or not,
because the compiler needs to know how to destroy the base parts of
derived objects.
> It seems to me the notion of over-riding a destructor is
> questionable. Maybe I am being overly concrete here, but when
> I over-ride a function I expect to see the same function name
> & the same parameters in the two classes.
Destructors don't have names. "classname::~classname()" is merely a
syntactic convention for specifying the unnamed function "the destructor
for this class." I think I see what you mean by "base class destructor"
now, however.
You don't override "base::~base()" as "derived::~base()"; there is no
function named "~base()," but rather an unnamed function "the
destructor" that you specify as "derived::~derived()" in the derived
class. This may be what's confusing you: structors don't have names.
> |I think there's even a Guru of the Week (from clc++.mod) that deals with
> |this subject.
> Thanks for the pointer.
Was there something useful there? I still haven't had a chance to check.
--
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
My reply address is correct as-is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]