Topic: Pure virtual destructors
Author: eds <edsantos@cxp.com>
Date: 1999/11/19 Raw View
Author: mckelvey@fafnir.com (James W. McKelvey)
Date: 1999/11/11 Raw View
The simple code below results in a link error with both GCC 2.95.1 and the CC
Sun compiler:
/bin/ld: Undefined symbols:
Pattern::~A(void)
Since it fails on two different compilers I'm hesitant to report it as a GCC
bug, but I can't see the problem. If I change the pure virtual function to
something other than a destructor, it works fine.
> >
> > class A
> > {
> > public:
> > virtual ~A(void) = 0;
> > };
> >
> > class B : public A
> > {
> > public:
> > virtual ~B(void)
> > {
> > };
> > };
> >
> > int main(void)
> > {
> > B x;
> >
> > return 0;
> > }
Checking the standard, 10.4 says" "A pure virtual function need be defined
only if explicitly called with the qualified-id syntax."
And 12.4(7) says "A destructor can be declared virtual or pure virtual; if
any objects of that class or any derived class are created in the program,
the destructor shall be defined."
This is somewhat of a contradiction. Does "shall be defined" mean "must be
defined"? Or does it mean that a default destructor will be invented by the
compiler?
--
Where diesel guitars from faraway bars, blast out the
best songs from our holy wars. Coyote carnival catches
on fire, all the cops in the world pick us up on radar.
Jim McKelvey mckelvey@fafnir.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: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/12 Raw View
"James W. McKelvey" wrote:
>
> Checking the standard, 10.4 says" "A pure virtual function need be defined
> only if explicitly called with the qualified-id syntax."
>
> And 12.4(7) says "A destructor can be declared virtual or pure virtual; if
> any objects of that class or any derived class are created in the program,
> the destructor shall be defined."
>
> This is somewhat of a contradiction. Does "shall be defined" mean "must be
> defined"? Or does it mean that a default destructor will be invented by the
> compiler?
The word "shall" in standardese means "undefined behavior if you
violate the requirement." In other words, it means "must".
Also notice 12.4/3, which says that the compiler defines a destructor
if there is no user-declared destructor. If you declare any function,
including constructors and destructors, you are responsible for
defining it.
--
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: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/11/12 Raw View
James W. McKelvey <mckelvey@fafnir.com> wrote in message
news:80dhge$hlp$2@ash.prod.itd.earthlink.net...
[snip]
> This is somewhat of a contradiction. Does "shall be defined" mean "must be
> defined"? Or does it mean that a default destructor will be invented by
the
> compiler?
Compiler is correct. The base class destructor is called when the object is
destroyed. You must define it even though it's purely virtual. There is no
contradiction, the standard uses the word "shall" as an imperative.
Gene Bushuyev
---
[ 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 ]