[comp.lang.c++] How to use Inheritance instead of nesting classes

heintzefmcsse.enet.dec.com (Sieg Heintze) (07/20/90)

I wanted to nest some classes inside of classes on Zortech C++ v2.01 and
found out that this cannot  be done like it can for pascal records. I ended
up using pointers to the (sub)classes.    Hmmph...   Upon driving to
work this morning it  dawned on me that this is what inheritance is for!

Well, well... I have finally found a use for inheritance - or have I?

I have some entities, viewports to be specific, that must be able to reside 
multiple doubly linked lists at once.  In otherwords, a view port might be 
both in  the "mouse button depress queue" and in the "mouse motion queue" 
at the same time.

For an entity to reside on a single list I could follow Bjarne's example in his
book just fine.  But how do I define an object that resides on multiple 
lists and use inheritance?  Can I inherit the same object twice in the 
same class?

				Sieg

heintze@genral.enet.dec.com
Digital Equipment Corporation
1110 Chapel Hills Drive  CXN2-2/35
Colorado Springs, CO 80920-3995
719-260-2184

kgorlen@sparkler.dcrt.nih.gov (Keith Gorlen) (07/21/90)

In article <13647@shlump.nac.dec.com> heintzefmcsse.enet.dec.com (Sieg Heintze) writes:
>I have some entities, viewports to be specific, that must be able to reside 
>multiple doubly linked lists at once.  In otherwords, a view port might be 
>both in  the "mouse button depress queue" and in the "mouse motion queue" 
>at the same time.
>
>For an entity to reside on a single list I could follow Bjarne's example in his
>book just fine.  But how do I define an object that resides on multiple 
>lists and use inheritance?  Can I inherit the same object twice in the 
>same class?

Chapter 13 of our new book, "Data Abstraction and Object-Oriented
Programming in C++", shows how to write a class that uses multiple
inheritance to inherit two instances of a base class, Link, so that
instances of it can reside on two *singly* linked lists at the same
time.  You could use the same approach for *doubly* linked lists, I'm
sure.  Briefly, the trick is that you must derive two classes from
class Link, say ButtonQLink and MotionQLink, them multiply derive your
class Viewport from these.  This is necessary so that you have some
way of disambiguating references to the two Link subobjects in a
Viewport object.

Reference:

"Data Abstraction and Object-Oriented Programming in C++"
Keith E. Gorlen, Sanford M. Orlow, and Perry S. Plexico
John Wiley & Sons

	ISBN 047192346X $39.25 (paperback)
	ISBN 047192752X $16.95 (NIH Class Library + examples, 2 disk set)
	ISBN 0471927511 $82.70 (cloth + 2 disk set)

--
	Keith Gorlen			phone: (301) 496-1111
	Building 12A, Room 2033		uucp: uunet!nih-csl!kgorlen
	National Institutes of Health	Internet: kgorlen@alw.nih.gov
	Bethesda, MD 20892

hansen@pegasus.ATT.COM (Tony L. Hansen) (07/23/90)

< I have some entities, viewports to be specific, that must be able to
< reside multiple doubly linked lists at once.  In otherwords, a view port
< might be both in  the "mouse button depress queue" and in the "mouse
< motion queue" at the same time.

< For an entity to reside on a single list I could follow Bjarne's example
< in his book just fine.  But how do I define an object that resides on
< multiple lists and use inheritance?  Can I inherit the same object twice
< in the same class?

The method which occurred to me several years ago when someone else asked me
the same question was to use multiple inheritance after introducing a pair
of intermediate classes which are used to differentiate between the two
lists.

Something like this should do the trick:

	class mouse_button_link : public slink
	    { ... };

	class mouse_button_queue : public slist
	    { ... };

	class mouse_motion_link : public slink
	    { ... };

	class mouse_motion_queue : public slist
	    { ... };

	class viewport :
		public mouse_botton_link,
		public mouse_motion_link
	    { ... };

Of course, this requires suitable definitions of slink and slist.

					Tony Hansen
				att!pegasus!hansen, attmail!tony
				    hansen@pegasus.att.com