🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #906

template base classes

#906
From: Ari Juhani Huttunen
Date: Tue, 05 Mar 1991 13:19
102 lines
5009 bytes
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.
_______________________________________________________________________________
Technical Details
Message-ID: <AHUTTUNE.91Mar5141930@bulldozer.hut.fi>
Path: arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!elroy.jpl.nasa.gov!sdd.hp.com!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!news.funet.fi!funic!santra!santra!ahuttune

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!elroy.jpl.nasa.gov!sdd.hp.com!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!news.funet.fi!funic!santra!santra!ahuttune
From: ahuttune@niksula.hut.fi (Ari Juhani Huttunen)
Newsgroups: comp.std.c++
Subject: template base classes
Message-ID: <AHUTTUNE.91Mar5141930@bulldozer.hut.fi>
Date: 5 Mar 91 13:19:30 GMT
Sender: news@santra.uucp (Cnews - USENET news system)
Distribution: comp
Organization: Helsinki University of Technology, Finland
Lines: 102