home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12905 < prev    next >
Encoding:
Text File  |  1992-08-25  |  4.2 KB  |  92 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mips!msi!snoopy!bill
  3. From: bill@snoopy (Bill Poitras)
  4. Subject: Re: backwards casting for lackof better term
  5. Message-ID: <1992Aug26.021725.10200@msi.com>
  6. Sender: usenet@msi.com (USENET)
  7. Reply-To: bill@msi.com
  8. Organization: Molecular Simulations, Inc.
  9. X-Newsreader: Tin 1.1 PL5
  10. References: <6370@lhdsy1.lahabra.chevron.com>
  11. Date: Wed, 26 Aug 1992 02:17:25 GMT
  12. Lines: 78
  13.  
  14. W.R. Volz (hwrvo@kato.lahabra.chevron.com) wrote:
  15. : Another simple question from someone who's simple:
  16. : Let's say I have a class A. And from that class I inherit and make
  17. : three other classes, say AA, AB and AC. Now I can have a pointer
  18. : to any of these classes: A *a ==> can point to any of the four classes.
  19. : Assume that I have a routine that creates one of the three derived classes at
  20. : random and returns it. So I might have
  21. : A *a[10];
  22. : for(int i=0;i<10;i++)
  23. :  a[i] = ____________ CreateAAorABorAC(i);
  24. : The question: what does CreateAAorAborAC return, or, what does the function
  25. : protptype looklike? A * CreateAAorABorAC(int) or what.
  26. : And inside of the function, what does it look like:
  27. [CreateAAorABorAC deleted]
  28.  
  29. What is presented looks correct.  A * CreateAAorABorAC(int) is a correct
  30. prototype.
  31.  
  32. : Is anything lost by casting the new object  back to A, or is the new object  
  33. : still one of the derived classes. Is this a problem unique to c++, due to 
  34. : strong static type checking? Can this function be written so that it is 
  35. : independant of any new classes that might be created from A, or, what 
  36. : happens if I create a new class AD.  If all the derived classes handle 
  37. : their initializations, can this routine be written so that it is 
  38. : independant of new classes that might be created later, like AD?
  39.  
  40. 1) Is anything lost?
  41. That depends.  If:
  42.     a) If your method interface relies on virtual functions, and you
  43.     don't need to case A * back to its original class, then no,
  44.     nothing is lost.
  45.     b) If you have a virtual function isA that tells which class the
  46.     object of type A points to, then you can do a switch statement to
  47.     do the cast.  This however is not very object oriented.
  48.     c) If you have no virtual functions to help the base object point
  49.     to the correct methods of the derived object, then all is lost.
  50.  
  51. If you don't understand what I mean about virtual functions, read about
  52. it and ask questions.  Virtual functions are a confusing point in C++
  53. for the novice.
  54.  
  55. 2) What if I create a new class AD from A?  Any functions which operate
  56. on A will always work on derived classes, as every derived class contains
  57. the information of its parent.
  58.  
  59. : C++ allows classes to have a branching structure, can the branches come
  60. : back together to form a single class? So if I have a base class X, and
  61. : derive three more XA, XB, and XC, can I then merge the three classes
  62. : back into one? Can inheritance be used to do this? So that XX : XA,XB,XC.
  63. : After all XA and XB and XC are all different kinds of X and so is XX.
  64.  
  65. What you have described is multiple inheritance.  Yes its possible.
  66. However, the problem is that XX now contains 3 separate copies of X,
  67. inherited from XA, XB, and XC.  Therefore you must (or is it should?)
  68. declare X to a be a virtual base class.  That way the X component of XX
  69. is only there once.
  70.  
  71. : Any insights here would be quote helpful in getting over the learning hump
  72. : for c++ (It feels like a programming version of Mount Everest).
  73.  
  74. If you are learning C++ on your own, I don't have much advice other than
  75. find yourself a very good tutorial.  If you are doing this for work, try
  76. to convince you boss that taking a short class (3-5 days) would be a
  77. better way to spend your time.  I learned a lot of minor C++ details that
  78. I find important from a 5 day class at Microsoft University.  It had
  79. hands-on experience as well as instruction.  It was great.
  80.  
  81. --
  82. +-----------------+-------------------------------+-------------------------+
  83. | Bill Poitras    | Molecular Simulations Inc.    | Tel (408)522-9229       |
  84. | (bill)          | Sunnyvale, CA USA             |                         | 
  85. |                 | FAX (408)732-0831             | bill@msi.com            |
  86. +-----------------+-------------------------------+-------------------------+
  87.  
  88.