home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16147 < prev    next >
Encoding:
Text File  |  1992-11-12  |  2.7 KB  |  87 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: object classes
  5. Message-ID: <1992Nov12.112944.23864@ucc.su.OZ.AU>
  6. Keywords: Object, DoubleList
  7. Sender: news@ucc.su.OZ.AU
  8. Nntp-Posting-Host: extro.ucc.su.oz.au
  9. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  10. References: <1992Nov9.172651.10696@dragon.acadiau.ca>
  11. Date: Thu, 12 Nov 1992 11:29:44 GMT
  12. Lines: 73
  13.  
  14. In article <1992Nov9.172651.10696@dragon.acadiau.ca> 870086t@dragon.acadiau.ca (Shannon Tremblay) writes:
  15. >
  16. >I am trying to build a graphics editor.  Just the basic shapes, each one 
  17. >to be treated as an object.
  18. >Sounds like a simple task, right?  I thought so.
  19. >
  20. >class Shape : public Object
  21. >{
  22. >public:
  23. >    Shape();
  24. >    ~Shape();
  25. >    
  26. >    virtual char* nameOf();            // pure virtual members 
  27. >    virtual classType isA();        // from class Object
  28. >    virtual void printOn( ostream& );
  29. >    virtual hashType hashValue();
  30. >
  31. >    virtual void Draw();
  32. >    virtual void Assign();
  33. >private:
  34. >    int    figure,
  35. >        color,
  36. >        style;
  37. >};
  38.  
  39.     You are using the old Borland Object library -- dont. It was
  40. designed for when there was only single inheritance. Be suspicious
  41. of 'classType isA()'.
  42.  
  43.     Here's a proper class IMHO for drawing:
  44.  
  45.     class drawable {
  46.     public:
  47.     virtual void Draw()const=0;
  48.     };
  49.  
  50. Derive classes from drawable. Iterating through a list or array,
  51. you can just call
  52.  
  53.     shape->Draw()
  54.  
  55. You dont need to know what shape you are drawing, indeed you mustnt
  56. know, thats the objects job.
  57.  
  58. >Also when I  return something it is of type Object and therefore I loose 
  59. >the member functions I created under Shape.
  60.  
  61.     Of course. Dont use this technique, it is a Smalltalk method
  62. which is appropriate for Smalltalk (being dynamic) but not C++ 
  63. (being static).
  64.  
  65. >
  66. >Does anyone know of source code that would demonstrate to me how to go about
  67. >creating an object, placing it in a list, and then being able to retrieve
  68. >the object, intact, as it was placed on the list, that is derived members 
  69. >still avialable.  As well as being able to iterate through the list,
  70. >accessing the derived functions as I go.  
  71. >
  72. >Or am I expecting too much???  I didn't think so...
  73.  
  74.     You are expecting the wrong thing. You should NOT be able to
  75. access functions special to each shape from the list. All the objects
  76. on the list MUST be treated the same. This ensures you can add ANY shape
  77. to the list, and that none of them are special.
  78.  
  79.     (Note, for a polymorphic list you need a list of pointers
  80. or references not actual objects.)
  81.  
  82. -- 
  83. ;----------------------------------------------------------------------
  84.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  85.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  86. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  87.