From: bill@snoopy (Bill Poitras)
Subject: Re: backwards casting for lackof better term
Reply-To: bill@msi.com
Organization: Molecular Simulations, Inc.
X-Newsreader: Tin 1.1 PL5
Date: Wed, 26 Aug 1992 02:17:25 GMT

W.R. Volz (hwrvo@kato.lahabra.chevron.com) wrote:
: Another simple question from someone who's simple:
: 
: Let's say I have a class A. And from that class I inherit and make
: three other classes, say AA, AB and AC. Now I can have a pointer
: to any of these classes: A *a ==> can point to any of the four classes.
: Assume that I have a routine that creates one of the three derived classes at
: random and returns it. So I might have
: 
: A *a[10];
: for(int i=0;i<10;i++)
:  a[i] = ____________ CreateAAorABorAC(i);
: 
: The question: what does CreateAAorAborAC return, or, what does the function
: protptype looklike? A * CreateAAorABorAC(int) or what.
: 
: And inside of the function, what does it look like:
[CreateAAorABorAC deleted]

What is presented looks correct.  A * CreateAAorABorAC(int) is a correct
prototype.

: Is anything lost by casting the new object  back to A, or is the new object  
: still one of the derived classes. Is this a problem unique to c++, due to 
: strong static type checking? Can this function be written so that it is 
: independant of any new classes that might be created from A, or, what 
: happens if I create a new class AD.  If all the derived classes handle 
: their initializations, can this routine be written so that it is 
: independant of new classes that might be created later, like AD?

1) Is anything lost?
That depends.  If:
	a) If your method interface relies on virtual functions, and you
	don't need to case A * back to its original class, then no,
	nothing is lost.
	b) If you have a virtual function isA that tells which class the
	object of type A points to, then you can do a switch statement to
	do the cast.  This however is not very object oriented.
	c) If you have no virtual functions to help the base object point
	to the correct methods of the derived object, then all is lost.

If you don't understand what I mean about virtual functions, read about
it and ask questions.  Virtual functions are a confusing point in C++
for the novice.

2) What if I create a new class AD from A?  Any functions which operate
on A will always work on derived classes, as every derived class contains
the information of its parent.

: C++ allows classes to have a branching structure, can the branches come
: back together to form a single class? So if I have a base class X, and
: derive three more XA, XB, and XC, can I then merge the three classes
: back into one? Can inheritance be used to do this? So that XX : XA,XB,XC.
: After all XA and XB and XC are all different kinds of X and so is XX.

What you have described is multiple inheritance.  Yes its possible.
However, the problem is that XX now contains 3 separate copies of X,
inherited from XA, XB, and XC.  Therefore you must (or is it should?)
declare X to a be a virtual base class.  That way the X component of XX
is only there once.

: Any insights here would be quote helpful in getting over the learning hump
: for c++ (It feels like a programming version of Mount Everest).

If you are learning C++ on your own, I don't have much advice other than
find yourself a very good tutorial.  If you are doing this for work, try
to convince you boss that taking a short class (3-5 days) would be a
better way to spend your time.  I learned a lot of minor C++ details that
I find important from a 5 day class at Microsoft University.  It had
hands-on experience as well as instruction.  It was great.

--
+-----------------+-------------------------------+-------------------------+
| Bill Poitras    | Molecular Simulations Inc.    | Tel (408)522-9229       |
| (bill)          | Sunnyvale, CA USA             |                         | 
|                 | FAX (408)732-0831             | bill@msi.com            |
+-----------------+-------------------------------+-------------------------+