home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!lhdsy1!kato.lahabra.chevron.com!hwrvo
- From: hwrvo@kato.lahabra.chevron.com (W.R. Volz)
- Newsgroups: comp.lang.c++
- Subject: backwards casting for lackof better term
- Message-ID: <6370@lhdsy1.lahabra.chevron.com>
- Date: 26 Aug 92 00:45:18 GMT
- Sender: news@lhdsy1.lahabra.chevron.com
- Organization: Chevron Oil Field Research Company
- Lines: 56
-
- 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:
-
- A * CreateAAorABorAC(int i)
- {
- A * ret;
- if(i % 3 == 0)
- ret = (A *) new AA;
- else if(i % 3 == 1)
- ret = (A *) new AB;
- else if(i % 3 == 2)
- ret = (A *) new AC;
- return ret;
- }
-
- 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?
-
- 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.
-
- Any insights here would be quote helpful in getting over the learning hump
- for c++ (It feels like a programming version of Mount Everest).
-
- Thanks, in advance.
-
- --
-
- ======================
- Bill Volz
- Chevron Oil Field Research Co.
- Exploration Research/Geophysics Division.
- P.O. Box 446, La Habra, CA 90633-0446
- Phone: (310) 694-9340
-