Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!ames!sgi!shinobu!odin!sgi.com!linton From: linton@sgi.com (Mark Linton) Newsgroups: comp.lang.c++ Subject: Re: Abstract Base Class Enforcement Keywords: class, abstract, c++ Message-ID: <1991Mar25.200522.3451@odin.corp.sgi.com> Date: 25 Mar 91 20:05:22 GMT References: <358@intertel.UUCP> Sender: news@odin.corp.sgi.com (Net News) Reply-To: linton@sgi.com (Mark Linton) Organization: sgi Lines: 27 In article <358@intertel.UUCP>, wohlenbe@intertel.UUCP (Greg Wohlenberg) writes: |> I've got a question. Say you have a base class that you want to be |> abstract. But, for one reason or another, it isn't convenient to make any of |> the class's virtual functions pure. (This would happen if all your virtual |> functions actually "do something" in the base class.) Given that you want to |> disallow instances of the abstract base class from being created, which of |> these alternatives is best? |> |> 1. Create a extra do-nothing function in the base class that is pure virtual. |> |> 2. Make the base class's no-parameter constructor "protected". This makes it |> so only derived classes are able to create instances of the base class (and |> therefore enforces the abstract characteristic of the base class). Use option 2, making all constructors protected (define a protected no-parameter constructor if the class otherwise has no constructors). Adding an unnecessary pure virtual would confuse people. Pure virtual means subclasses must provide an implementation, which is not what you intend. Similarly, a pure virtual destructor, if that works, is not what you want (you needn't force subclasses to define a destructor). Unfortunately, section 10.3 of the ARM confuses people with the statement A class is abstract if it has at least one pure virtual function. This statement is correct, but many people interpret the "if" to mean "if and only if", which is not correct. All classes with pure virtual functions are abstract, but not all abstract classes have a pure virtual function.