Newsgroups: comp.lang.c++
Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!geac!alias!rae
From: rae@alias.com (Reid Ellis)
Subject: class design [WAS: Calling Parent functions in Child]
Message-ID: <1991May24.052212.27585@alias.com>
Sender: news@alias.com (0000-news(0000))
Organization: Alias Research, Inc., Toronto ON Canada
References: <91132.113932ACPS2924@Ryerson.Ca> <1991May15.044458.21518@minyos.xx.rmit.oz.au> <3022@moth.sw.mcc.com>
Date: Fri, 24 May 1991 05:22:12 GMT

[ My apologies if this is seen twice -- I had some problems with our
  news server recently ]

Bossman <s892992@minyos.xx.rmit.oz.au> writes:
|To call A's version of isEqual, you need to use scope resolution. You
|can do this as follows:
|
|B::isEqual()
|{
|    return (... && A::isEqual() );
|}

Rob Pettengill <rcp@moth.sw.mcc.com> writes:
|This technique does not work for "mixin" style classes with multiple
|inheritance. [...] mixins that seek to augment existing member
|functions run into the following difficulty.
|
|For an over simplified examaple -  one might want to write a
|MetricWeightMixin that could be added to any class that returned an
|English Weight.
|
|class MetricWeightMixin : {
|public:
|  virtual float weight();
|}
|
|MetricWeightMixin::weight()
|{
|  return lbsToKilos( DontKnowWhatClassToPutHere::weight());
|}

The problem is that you are going about solving this problem the wrong
way.  What you would want in this situation is to build classes
representing the units involved and include copy constructors as
appropriate. e.g.:

	class lbs;	// imperial weight [pounds]
	class kg;	// metric weight [kilograms]

	// the following line is illegal, but I don't want to write
	// a whole class just for this example -- you get the idea.

	extern lbs X::weight();		// X only knows imperial measure

	X x;
	kg metricWeight = x.weight();	// calls kg::kg(const lbs &)

No mixin required.  As an alternative, you could instead provide
conversion operators for you classes to cast themselves to the
appropriate type.  For instance, instead of the constructor

	kg::kg(const lbs &);

you could have the operator

	lbs::operator kg();

In this way, *you* can choose who knows about whom -- does kg know
about lbs or does lbs know about kg?  If the relationship is one-way,
as would be the case for a distributed class library that you wouldn't
want to modify, then you can have both methods.  If you were writing
the kg class, for instance, you could have both of:

	kg::kg(const lbs &);	// convert pounds to kilos
	kg::operator lbs();	// convert kilos to pounds
	
						Reid

--
Reid Ellis
rae@utcs.toronto.edu        ||               rae@alias.com
CDA0610@applelink.apple.com ||      +1 416 362 9181 [work]
