home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: object classes
- Message-ID: <1992Nov12.112944.23864@ucc.su.OZ.AU>
- Keywords: Object, DoubleList
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <1992Nov9.172651.10696@dragon.acadiau.ca>
- Date: Thu, 12 Nov 1992 11:29:44 GMT
- Lines: 73
-
- In article <1992Nov9.172651.10696@dragon.acadiau.ca> 870086t@dragon.acadiau.ca (Shannon Tremblay) writes:
- >
- >I am trying to build a graphics editor. Just the basic shapes, each one
- >to be treated as an object.
- >Sounds like a simple task, right? I thought so.
- >
- >class Shape : public Object
- >{
- >public:
- > Shape();
- > ~Shape();
- >
- > virtual char* nameOf(); // pure virtual members
- > virtual classType isA(); // from class Object
- > virtual void printOn( ostream& );
- > virtual hashType hashValue();
- >
- > virtual void Draw();
- > virtual void Assign();
- >private:
- > int figure,
- > color,
- > style;
- >};
-
- You are using the old Borland Object library -- dont. It was
- designed for when there was only single inheritance. Be suspicious
- of 'classType isA()'.
-
- Here's a proper class IMHO for drawing:
-
- class drawable {
- public:
- virtual void Draw()const=0;
- };
-
- Derive classes from drawable. Iterating through a list or array,
- you can just call
-
- shape->Draw()
-
- You dont need to know what shape you are drawing, indeed you mustnt
- know, thats the objects job.
-
- >Also when I return something it is of type Object and therefore I loose
- >the member functions I created under Shape.
-
- Of course. Dont use this technique, it is a Smalltalk method
- which is appropriate for Smalltalk (being dynamic) but not C++
- (being static).
-
- >
- >Does anyone know of source code that would demonstrate to me how to go about
- >creating an object, placing it in a list, and then being able to retrieve
- >the object, intact, as it was placed on the list, that is derived members
- >still avialable. As well as being able to iterate through the list,
- >accessing the derived functions as I go.
- >
- >Or am I expecting too much??? I didn't think so...
-
- You are expecting the wrong thing. You should NOT be able to
- access functions special to each shape from the list. All the objects
- on the list MUST be treated the same. This ensures you can add ANY shape
- to the list, and that none of them are special.
-
- (Note, for a polymorphic list you need a list of pointers
- or references not actual objects.)
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-