[comp.lang.c++] method specification in abstract classes, a problem...

niklas@appli.se (Niklas Hallqvist) (10/09/90)

One of the most useful properties of c++ when designing class libraries
is the concept of abstract classes.  Using pure virtual functions assures
you that every (useful) derived class implements at least the methods you've
specified in your abstract base class.  But... (there's always one)...
why can't I specify that certain operations should be closed on their
domain?  Suppose you have an abstract class which should specify
that all derived classes should implement an operator+() closed on
themselves, how would you do that?  Note that I talk about static (compile
time) typing, not dynamic.  I want the compiler to flag an error if a derived
class does not implement this operator.  Consider this (non-c++) code:

class Addable {
public:
  virtual class() = 0;			// A VIRTUAL constructor!!!
  virtual class& operator+(class&) = 0;	// An operator closed on its domain.
};

class AddableShort : Addable {
private:
  short x;
public:
  AddableShort(short y = 0) { x = y; }
  AddableShort& operator+(AddableShort& y) { return AddableShort(x + y); }
};

As you can see, I've overloaded the keyword class in an abstract base class
context to mean: the class where the method being declared is defined.
My point here is, as the designer of the abstract base class, I want to ensure
that I can create some derived Addable with no arguments, and that applying
the operator+ to two objects of the same static type (derived from Addable)
should return another object of that type.  When I've ensured this, I can
distribute implementation work to other programmers, knowing my specification
will be followed.

So, flame me, is this just a silly thought of me?  Is there another way
getting what I want?  I'm just getting more and more confused, thinking
of this...  Could templates help me?

					Bring me clarity!
					Niklas
-- 
Niklas Hallqvist                Phone: +46-(0)31-19 14 85
Applitron Datasystem            Fax:   +46-(0)31-19 80 89
N. Gubberogatan 30              Email: niklas@appli.se
S-416 63  GOTEBORG, Sweden             sunic!chalmers!appli!niklas