Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!sbi!pivot-sts!swampthing!wfl From: wfl@swampthing.sbi.com (W. Linke CPE) Newsgroups: comp.lang.c++ Subject: Pure virtual destructors are illegal? Keywords: virtual destructor Message-ID: <183@swampthing.sbi.com> Date: 31 May 91 23:50:54 GMT Organization: Salomon Brothers - NJ Lines: 44 It seems that you can't have a pure virtual destructor, but the compiler (Sun SPARCstation cfront 2.00.02) doesn't object. Consider the following: class Base { public: Base() { } virtual ~Base() = 0; // bad! }; class Derived : public Base { public: Derived() { } ~Derived() { } }; main() { Derived instance; } This compiles successfully, but when you try to link a load module from it the linker fails, saying it can't find " ___dt__4BaseFva ". This actually makes sense, because a virtual destructor is different from other virtual functions. When the Derived 'instance' is destroyed, both ~Derived() and ~Base() will be called, just as with a non-virtual destructor. What 'virtual' gives you is correct destruction even when the object is accesed via a base class pointer, such as: Base *bp = new Derived; delete bp; // both ~Derived() and ~Base() called whereas if the destructor weren't virtual, only ~Base() would be called. But by declaring ~Base() to be pure virtual, there actually is no ~Base() routine, so the linker reports it as an undefined symbol. It seems to me that the compiler should either flag pure virtual destructors as syntax errors, or generate an internal "null" ~Base() destructor for classes derived from Base which provide their own destructor. Do other compilers/versions catch this problem? Bill Linke uunet!sbi!gort!wfl