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