Topic: Simple solution already exists,


Author: David <nospam@nospam.com>
Date: Sun, 14 Jan 2001 18:05:00 GMT
Raw View
To create an implementation of an inline, pure virtual function, the
following works:
class C
{
    virtual ~C() = 0 ;
};
inline C::~C()
{
    cout << "Invoked ~C()" << endl ;
}
class D: public C
{
    ~D(){ cout << "Invoked ~D()" << endl ;
}
Note that I selected the destructor advisedly, as this method of making a
base class abstract does not require the use of an otherwise superfluous
pure virtual function, and also demonstrates a case in which the pure
virtual function will definitely be invoked by a derived class, since the
destructor must be invoked by the derived class destructor.

    David
    boktroktr+zsk-hmsk+bnk
    To get actual address from obfuscated address, cycle each letter up one
and change the two plus signs to at and period, respectively.


mgradman@my-deja.com wrote:

   Allowing an implementation for a pure virtual function at all would
   contradict the true meaning of "pure virtual".  Pure virtuals are only
   supposed to serve as an abstract placeholder which must be implemented
   in derived classes that will be instantiated.  One should not be able
   to provide an implementation of a pure virtual within the class it is
   defined.  Pure virtuals are abstract methods that serve as polymorphic
   interfaces to the derived classes and nothing more.  And remember, the
   class in which the pure virtual is defined may only be used
   polymorphically (no objects of this *abstract* class may be
   instantiated; they may only be referred to through pointers or
   references).  If you really need to define an implementation for such
   functions, they should not even be pure virtual ... just declare them
   as "virtual" and then you may provide your implementation.  To emulate
   the "abstractness" of the class, just declare a protected pure virtual
   dummy function which your concrete subclasses implement with an empty
   function body:

   // can't be instantiated due to dummy() being pure virtual
   class AbstractBase
   {
   public:
      virtual void PureVirtual() { // ... }

   protected:
      virtual void dummy() = 0;  // preserves abstractness of AbstractBase
   };

   class ConcreteDerived : public AbstractBase
   {
   public:
       // override PureVirtual() here if desired

   protected:
      virtual void dummy() { }  // "flags" this class as concrete
   };

   // remains abstract as dummy() not implemented
   class AbstractDerived : public AbstractBase
   {
   public:
       // override PureVirtual() here if desired
   };

   class ConcreteMostDerived : public AbstractDerived
   {
   public:
       // override PureVirtual() here if desired

   protected:
       virtual void dummy() { } // "flags" this class as concrete
   };

   The use of these classes is just as you would expect.

   If your classes have true pure virtual functions besides the "pure
   virtual" that you wish to actually implement in your class, then you
   don't need to use the pure virtual dummy() function in them.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]