home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13148 < prev    next >
Encoding:
Text File  |  1992-09-01  |  3.5 KB  |  84 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!eff!news.byu.edu!ux1!fcom.cc.utah.edu!swillden
  3. From: swillden@news.ccutah.edu (Shawn Willden)
  4. Subject: Re: Getting type info back !
  5. Message-ID: <1992Sep1.150027.26050@fcom.cc.utah.edu>
  6. Sender: news@fcom.cc.utah.edu
  7. Organization: University of Utah Computer Center
  8. X-Newsreader: Tin 1.1 PL3
  9. References: <YeckhJS00awJ0JJJV7@andrew.cmu.edu>
  10. Date: Tue, 1 Sep 92 15:00:27 GMT
  11. Lines: 71
  12.  
  13. sm86+@andrew.cmu.edu (Stefan Monnier) writes:
  14. : I would like to store different type of objects in a 'generic' structure,
  15. : using the (void *)
  16. : The problem I'm faced with is that of determining the real type of an 
  17. : object after storing in that structure (when I take it back)
  18. : It is a quite general question.
  19.  
  20. [ Description of question and Eiffel support deleted ]
  21.  
  22. Putting aside for the moment the question of whether or not you should want
  23. to create heterogeneous collections (I try very hard to avoid them), what you
  24. want can be done, but you have to do some work.  The problem is that C++ 
  25. doesn't (currently) provide any type of run-time type information (RTTI) so
  26. you must provide it yourself if you need it.  Here are a couple of the most
  27. common ways to handle this problem:
  28.  
  29.     1)  Derive all classes you want to be able to place in your structure
  30.     from a common base class, I'll call it Generic.  This base class
  31.     should have a pure virtual method that descendants will override to
  32.     provide type information (e.g. an integer that uniquely specifies
  33.     the class).  If you want to be able to use sorted data structures
  34.     you should also provide a pure virtual function lessThan (or some-
  35.     thing to that effect).  Then, instead of handling void*'s in your
  36.     structure, work with Generic*'s.  Then you can do something like:
  37.  
  38.     void foo(SomeDataStructure& data)
  39.         {
  40.         Generic* found;
  41.         MyClass myObject;    // Suppose MyClass is descended from
  42.                     // Generic.
  43.         found = data.getItem();
  44.         if (found->classType() == myObject.classType())
  45.             myObject = *found;
  46.         // ...
  47.         }
  48.     2)  If you *really* want to work with void*'s you can place your
  49.     void* in a struct along with some type information.  Every time you
  50.     add an object to your data structure you will have to pass both the
  51.     pointer to the object and the type information to the structure which
  52.     must store them together.  Then, when you retrieve the object, you
  53.     also must get the type info and use a switch to do the appropriate 
  54.     cast.  This allows you to avoid mucking with your inheritance 
  55.     hierarchy but is extremely easy to mess up.  Mistakes can be disastrous
  56.     and extremely hard to find.
  57.  
  58. There are many other ways of doing this but they all boil down to:
  59.  
  60.     C++ does *not* provide RTTI.  If you want it you must provide
  61.     it yourself.
  62.  
  63. In general, I prefer to use the 1st method I mentioned when I have to have 
  64. heterogeneous collections.  If at *all* possible I use class templates for
  65. my generic structures and make homogeneous collections.  If I find myself
  66. wanting to place very different types of objects in the same pile I step 
  67. back and ask myself why I want to do that and whether or not those objects
  68. really have anything in common.  99% of the time there is a better way.
  69. (Things like OODBMSs and class browsers are examples of things in the other
  70. 1% - they do exist but they are rare.)
  71.  
  72. : -----------------------------------------------------
  73. : -- On the average, people seem to be acting normal --
  74. : -----------------------------------------------------
  75.  
  76. On the average, most people normally do.
  77.  
  78. --
  79. Shawn Willden
  80. swillden@icarus.weber.edu
  81.