The following code: class A { public: virtual ~A() = 0; }; class B : public A { public: ~B() {} }; int main() { B b; } compiles with GCC 4.0.2 (clean with -ansi -Wall -Wextra) but does not link due to an undefined reference to ~A(). Herb Sutter claims this code is valid, for whatever that might be worth (http://www.gotw.ca/gotw/031.htm), but in either case this seems to be a bug; either it should be rejected with a diagnostic as invalid code, or it should link (and ideally do something sensible).
You still need to declare A::~A(). That is what the following passage from that doc means: Of course, any derived class' destructor must call the base class' destructor, and so the destructor must still be defined (even if it's empty): // file b.cpp B::~B() { /* possibly empty */ } If this definition were not supplied, you could still derive classes from B but they could never be instantiated, which isn't particularly useful.
Ah, I misread it, but the bug should stay open IMO - the invalidity of the code reduces it to "GCC doesn't reject invalid code", which is obviously a low priority, but still a bug, no?
(In reply to comment #2) > Ah, I misread it, but the bug should stay open IMO - the invalidity of the code > reduces it to "GCC doesn't reject invalid code", which is obviously a low > priority, but still a bug, no? No, this is not invalid code. Just useless code.
Subject: Re: New: "pure virtual" destructors accepted by GCC, but cause link failure "lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes: | The following code: | | class A | { | public: | virtual ~A() = 0; You still need to *define* the destructor. See ยง12.4/7. -- Gaby
Subject: Re: "pure virtual" destructors accepted by GCC, but cause link failure "lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes: | Ah, I misread it, but the bug should stay open IMO - the invalidity | of the code reduces it to "GCC doesn't reject invalid code", which | is obviously a low priority, but still a bug, no? the code is rejected -- at link time. So it is no bug. PR should be closed as invalid. -- Gaby
Thank you for the reference Gaby. I'm now not quite sure what purpose a pure virtual destructor has, or why it should be legal, but neither the apparent language oddity nor my confusion about same is a GCC problem, so... Andrew, Gaby, sorry to distract you with the invalid bug report. I'll check the standard first next time.
Subject: Re: "pure virtual" destructors accepted by GCC, but cause link failure "lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes: | I'm now not quite sure what purpose a pure virtual destructor has, the usefulness used to be subject of debate when OO was "the" main topic. Some people believe it is a concise way for them to state that a given base class for a hierarchy is abstract. -- Gaby