home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19668 < prev    next >
Encoding:
Internet Message Format  |  1993-01-25  |  2.2 KB

  1. Xref: sparky comp.lang.c++:19668 comp.object:4968
  2. Path: sparky!uunet!pipex!warwick!uknet!edcastle!eanv20
  3. From: eanv20@castle.ed.ac.uk (John Woods)
  4. Newsgroups: comp.lang.c++,comp.object
  5. Subject: Re: HELP : Array of objects of diff classes ??
  6. Message-ID: <30588@castle.ed.ac.uk>
  7. Date: 21 Jan 93 13:23:31 GMT
  8. References: <1jdgecINNp06@aurora.engr.LaTech.edu> <1993Jan19.112059.2882@ucc.su.OZ.AU>
  9. Organization: Edinburgh University
  10. Lines: 40
  11.  
  12. vinay> I have a problem related to programming in an object oriented fashion.
  13. vinay>    Here`s the problem description :
  14. vinay>    There is an object class called Holder. The object holder can
  15. vinay> hold various things : Pencils, Pens, Erasers etc. I`ve declared
  16. vinay> different classes for these things ( class PENCIL, class PEN,
  17. vinay> class ERASER etc. ).  
  18. vinay> The user of this program will choose at the runtime what objects to put
  19. vinay> in the holder. The problem is how do i store this information (i.e the
  20. vinay> contents ) in class HOLDER. I cannot create a single array of objects
  21. vinay> because each object is of different class.
  22.  
  23. maxtal>    Forget Object Oriented Fashions, whatever they are.
  24.  
  25. I'm not sure that's such a good idea, myself :-)
  26.  
  27. maxtal>    Use a discriminated union. Oh, C++ doesn't have one,
  28. maxtal>so emulate one with an idiom.  Below I use pointers for 
  29. maxtal>illustration, you can put copies of the actual objects in
  30. maxtal>if you like (just get rid of appropriate *'s)
  31.  
  32.     [ code deleted ]
  33.  
  34. maxtal>Do not be scared of using a switch. 
  35.  
  36. I'm not sure that's such a good idea, either.  I'd avoid them like the
  37. plague.  My own feeling as to what would constitute an approach in the
  38. `object oriented fashion' follows:
  39.  
  40. All things, like `pen', `eraser', `pencil' etc.  share the property
  41. that they can be put in the holder.  So, define class HolderTool.
  42. Then define subclasses of this, such as Pencil, Pen and Eraser.  Then
  43. one's holder is simply an array of HolderTool instances. 
  44.  
  45. What language are you using, BTW?
  46.  
  47.                 ... John Woods
  48. -- 
  49.  /******* cut here ******* John Woods ******* cut here ********
  50.  * Philosophy: Forsan et haec olim meminisse iuvabit (Virgil) *
  51.  * Disclaimer: Every statement in this file is possibly !true *
  52.  ******** cut here ******* John Woods ******* cut here *******/
  53.