Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!att!dptg!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.std.c++ Subject: Re: Pure virtual destructors: good or bad idea? Summary: Pure virtual destructors can be declared but then they must be defined too Message-ID: <11333@alice.UUCP> Date: 14 Sep 90 14:43:54 GMT References: <77210003@hpclscu.HP.COM> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 37 In article <77210003@hpclscu.HP.COM>, shankar@hpclscu.HP.COM (Shankar Unni) writes: > PURE VIRTUAL DESTRUCTORS: > > One of our users came up with an interesting construct: > > class Base { > virtual ~Base() = 0; // pure virtual destructor virtual void f() = 0; // example pure virtual function > }; > > class Derived { /*...*/ }; If you want to do this you must define the Base destructor, e.g. Base::~Base() {} It is a little-known (and perhaps unfortunate) fact that pure virtual functions can be defined. They can only be called through the explicitly qualified name, e.g., Base* bp = new Derived; bp->Base::f(); would call Base::f() if it has been defined, and cause a link error otherwise. The destructor is a slightly special case (:-)), since the compiler automatically generates the equivalent of an explicitly qualified call to it in the destructor of the derived class. An alternative way to define the language would be to disallow defining pure virtual functions and (therefore) disallow pure virtual destructors. This would give the programmer somewhat less flexibility but would not leave the compiler wondering whether a pure virtual function was going to be defined or not. -- Jonathan E. Shopiro AT&T Bell Laboratories, Warren, NJ 07059-0908 shopiro@research.att.com (201) 580-4229