Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: Abstract Base Class Enforcement Keywords: class, abstract, c++ Message-ID: <1991Mar24.204943.9998@world.std.com> Date: 24 Mar 91 20:49:43 GMT References: <358@intertel.UUCP> Organization: The World Public Access UNIX, Brookline, MA Lines: 34 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.) If you mean that it's all right for a derived class to override none of the base class's virtual functions, then you do need one of the additional methods you described or the one I'll mention below. However, if you mean that you need a definition in the base class to provide some common processing that can be used by derived class overriding functions, there's nothing to stop you from providing a definition for a pure virtual function; it will never be invoked by an ordinary member function call, but an explicitly-qualified call (e.g., "Base::pure_virt1()") will invoke the base class definition. > 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). Actually, you'd need to make *all* the constructors either protected or private. > Or, is there another alternative that I am not aware of? How about making the destructor pure? (I assume it's already virtual; the rule of thumb is to make the destructor virtual if there are any virtual functions.) You still have to provide a definition of it or the program won't link or run, of course. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com