home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12883 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  2.3 KB

  1. Path: sparky!uunet!lhdsy1!kato.lahabra.chevron.com!hwrvo
  2. From: hwrvo@kato.lahabra.chevron.com (W.R. Volz)
  3. Newsgroups: comp.lang.c++
  4. Subject: backwards casting for lackof better term
  5. Message-ID: <6370@lhdsy1.lahabra.chevron.com>
  6. Date: 26 Aug 92 00:45:18 GMT
  7. Sender: news@lhdsy1.lahabra.chevron.com
  8. Organization: Chevron Oil Field Research Company
  9. Lines: 56
  10.  
  11. Another simple question from someone who's simple:
  12.  
  13. Let's say I have a class A. And from that class I inherit and make
  14. three other classes, say AA, AB and AC. Now I can have a pointer
  15. to any of these classes: A *a ==> can point to any of the four classes.
  16. Assume that I have a routine that creates one of the three derived classes at
  17. random and returns it. So I might have
  18.  
  19. A *a[10];
  20. for(int i=0;i<10;i++)
  21.  a[i] = ____________ CreateAAorABorAC(i);
  22.  
  23. The question: what does CreateAAorAborAC return, or, what does the function
  24. protptype looklike? A * CreateAAorABorAC(int) or what.
  25.  
  26. And inside of the function, what does it look like:
  27.  
  28. A * CreateAAorABorAC(int i)
  29. {
  30. A * ret;
  31. if(i % 3 == 0)
  32.  ret = (A *) new AA;
  33. else if(i % 3 == 1)
  34.  ret = (A *) new AB;
  35. else if(i % 3 == 2)
  36.  ret = (A *) new AC;
  37. return ret;
  38. }
  39.  
  40. Is anything lost by casting the new object  back to A, or is the new object  
  41. still one of the derived classes. Is this a problem unique to c++, due to 
  42. strong static type checking? Can this function be written so that it is 
  43. independant of any new classes that might be created from A, or, what 
  44. happens if I create a new class AD.  If all the derived classes handle 
  45. their initializations, can this routine be written so that it is 
  46. independant of new classes that might be created later, like AD?
  47.  
  48. C++ allows classes to have a branching structure, can the branches come
  49. back together to form a single class? So if I have a base class X, and
  50. derive three more XA, XB, and XC, can I then merge the three classes
  51. back into one? Can inheritance be used to do this? So that XX : XA,XB,XC.
  52. After all XA and XB and XC are all different kinds of X and so is XX.
  53.  
  54. Any insights here would be quote helpful in getting over the learning hump
  55. for c++ (It feels like a programming version of Mount Everest).
  56.  
  57. Thanks, in advance.
  58.  
  59. -- 
  60.  
  61. ======================
  62. Bill Volz
  63. Chevron Oil Field Research Co.
  64. Exploration Research/Geophysics Division.
  65. P.O. Box 446, La Habra, CA 90633-0446
  66. Phone: (310) 694-9340
  67.