Topic: template base classes


Author: jimad@microsoft.UUCP (Jim ADCOCK)
Date: 11 Mar 91 23:32:36 GMT
Raw View
Yes.  This is the kind of idea I was suggesting when some time ago
when I said I thought the run-time type problem was really a specific
case of a template problem.  Template ought to be able to do run-time
type checking problems cleanly -- along with other class-derivation
problems.




Author: tom@ssd.csd.harris.com (Tom Horsley)
Date: 5 Mar 91 16:00:06 GMT
Raw View
>NormalLamp and MagicalLamp have a way to store some class specific information
>that is local to that class, but is only defined in the base class.
>THERE WAS NO NEED FOR THE PROGRAMMER OF THE DERIVED CLASSES TO KNOW HOW
>THIS INFORMATION IS STORED AND THERE WAS NO 'EXTRA WORK'.

>Using traditional C++ programming facilities the programmer would have been
>forced to write the static members in the derived classes by hand.

I have not been following the c++ standard very closely, but I had always
assumed this sort of thing would be possible with templates. (For a more
useful example than nethack, you might want to look at the convolutions the
NIH library has to go through with macros used to define classes).

If this is really not possible with the current template proposals, I am
all in favor of adding something to allow it. I have often wanted some way
to automatically derive new static members for classes (consider the use
for something like a cache for class specific information).
--
======================================================================
domain: tahorsley@csd.harris.com       USMail: Tom Horsley
  uucp: ...!uunet!hcx1!tahorsley               511 Kingbird Circle
                                               Delray Beach, FL  33444
+==== Censorship is the only form of Obscenity ======================+
|     (Wait, I forgot government tobacco subsidies...)               |
+====================================================================+




Author: ahuttune@niksula.hut.fi (Ari Juhani Huttunen)
Date: 5 Mar 91 13:19:30 GMT
Raw View
Situation: I have found that in many cases I have a base class from which
a large number of other classes have been derived. In _each_ of the derived
classes I need a piece of information that is, in a sense, identical. By this
I mean the information is known when the _base_ class is written.

Using the current C++ rules I'm forced to write this information in each
of the derived classes, thus doing extra work. When I first read about
templates, they seemed an ideal way to solve this, but the hope soon vanished..

In the following example I give a way to enhance templates so this extra
work to be done by the programmer is done automatically by the compiler.
This seems also a perfect way for a previously compiled library function
to store the information it needs in each of the derived classes, if any.
Either way, the programmer who creates the derived class need not be
concerned with this.

The example that follows is usable in a game not unlike nethack, though
I am certain it can be applied to more 'serious uses'. In nethack you
don't know _exactly_ which object is which until you identify them. (How
this is done need not concern the readers of this news group.)

The important point is that identifying _any_ object of a certain class
identifies _all_ objects of this class, but not of any other class.
The natural way to implement this is with a static variable of a suitable type.
This static variable _must_ be present in every derived class and since
this is so, it could be defined in the base class. The following program
gives an example. (Commentary follows after the code.)

class Lamp {
 template static char *real_name;
 template static char *general_name;
 template static int flag;
public:
 template virtual void set_flag()
  { flag = 1; }
 template<Lamp> Lamp()
  { flag = 0; real_name = general_name = ""; }
 template virtual char *get_description()
  { if(flag) return real_name; else return general_name; }
};

class NormalLamp : public Lamp {
 NormalLamp : real_name("A normal lamp"), general_name("A lamp") {}
};

//The initialization of NormalLamp::real_name is perhaps not very good
//programming practise, but I want to keep this clear.

class MagicalLamp : public Lamp {
 MagicalLamp : real_name("A magical lamp"), general_name("A lamp") {}
};

main()
{
 Lamp *lamp1 = new NormalLamp;
 Lamp *lamp2 = new MagicalLamp;
 Lamp *lamp3 = new MagicalLamp;

 // lamp1->get_description() == "A lamp"
 // lamp2->get_description() == "A lamp"
 // lamp3->get_description() == "A lamp"

 lamp2->set_flag();

 // lamp1->get_description() == "A lamp"
 // lamp2->get_description() == "A magical lamp"
 // lamp3->get_description() == "A magical lamp"
}

These base class templates follow these rules:

1) A template base class is a class that can be used only as a base class
   of some other class. Any class that has template members is a template
   base class. (In fact this rule might be unnecessary.)

2) Template members of a class are instantiated in every derived class
   and are thus ordinary members of this derived class.

3) template<Lamp> Lamp(); creates a constructor template that is created
   in each of the derived classes and that is called _before_ the derived
   class'es constructor. template<Lamp> is changed to template<NormalLamp>
   in the derived class NormalLamp.

4) No runtime overhead is created since templates can be resolved at
   compile time.

In the example real_name, general_name and flag are created in both of
the classes NormalLamp and MagicalLamp. What have we gained?

NormalLamp and MagicalLamp have a way to store some class specific information
that is local to that class, but is only defined in the base class.
THERE WAS NO NEED FOR THE PROGRAMMER OF THE DERIVED CLASSES TO KNOW HOW
THIS INFORMATION IS STORED AND THERE WAS NO 'EXTRA WORK'.

Using traditional C++ programming facilities the programmer would have been
forced to write the static members in the derived classes by hand.
--
___  ___  ___  ___  ___  ___  ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I__I I_
Ari.Huttunen@hut.fi  A computer is like a house of cards. Not as reliable,
                     but it has an equal number of incompatible parts.
_______________________________________________________________________________