Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: Pure virtual destructors are illegal? Message-ID: <1991Jun4.134117.21491@world.std.com> Date: 4 Jun 91 13:41:17 GMT References: <183@swampthing.sbi.com> <1410006@hpsciz.sc.hp.com> Organization: Glockenspiel, Ltd. Lines: 34 masa@hpsciz.sc.hp.com (Masayoshi Habu) writes: > Please correct me if I am wrong. A pure virtual function usually has no > implementation because the intention is to catch a run-time error if this > function is never overridden and called somehow. No, that's not quite right. The basic idea of a pure virtual function is to prevent its class from being instantiated, i.e., to make it abstract. Since derived classes that do not override a pure virtual function inherit the base class's pure virtual, it is impossible to create an object for which a pure virtual function is not overridden. The compiler guarantees that at compile time, not at runtime. A pure virtual function may indeed be defined, if it is to be called (as a destructor will be). Also, an implementation is not required to generate a runtime error if an undefined pure virtual function is invoked; E&S says only that such an occurrence results in undefined behavior (although an implementation that did not produce an error message would be less useful than one that did). > So my opinion > is that the compiler should reject a pure virtual destrcutor. Although I've never needed one, the rationale I've heard for pure virtual destructors was that someone wanted to make a class abstract but that all the ordinary member functions needed to be defined, i.e., not pure. Making the destructor pure virtual is a better choice than defining a dummy pure virtual that is unneeded except for making the class abstract. In any case, I see no need to make destructors a special case, and the core language working group of X3J16, the ANSI C++ Standard Committee, has explicitly affirmed that pure virtual destructors are intended to be included in the language. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com