home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.amiga.programmer
- Path: fuw.edu.pl!cyfronet!student!iskra
- From: iskra@student.uci.agh.edu.pl (Kamil Iskra)
- Subject: Re: Return types and virtual methods.
- Message-ID: <DMIE92.9Lv@cyf-kr.edu.pl>
- Sender: news@cyf-kr.edu.pl (News Administrator)
- Nntp-Posting-Host: student.uci.agh.edu.pl
- Organization: Academic Computer Centre, CYFRONET
- X-Newsreader: TIN [version 1.1 PL8]
- References: <4f4qbv$q18@columba.udac.uu.se>
- Date: Fri, 9 Feb 1996 12:48:38 GMT
-
- Daniel Widenfalk (t94dwi@chaph.tdb.uu.se) wrote:
- > class Set {
- > public:
- > virtual ~Set();
-
- > virtual Set & operator=(const Set &) = 0;
- > virtual Set operator~() = 0;
- > ...
- > };
-
- > class BitSet : public set {
- > public:
- > ~BitSet();
-
- > BitSet & operator=(const BitSet &);
- > BitSet operator~();
- > ...
- > };
-
- > Now, my question is. How do I fix this? I want to be able to have several
- > diffrent representations of sets, but I want them to look the same to the
- > user.
-
- I've seen similar question on comp.lang.c++.moderated (which would be more
- appropriate for your question, BTW). Here's one of the answers for that
- question:
-
- From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
- Newsgroups: comp.lang.c++.moderated
- Subject: Re: How to implement virtual operator== w/o RTTI?
- Date: 5 Feb 1996 03:41:44 -0000
-
- Phil Verghese wrote:
- >
- > I'm trying to implement a virtual operator==, and I'd like to avoid
- > having to use RTTI in the derived classes.
-
- Forget it. It cannot work. Any attempt to to even
- try to do it is doomed to be a perversion.
-
- It is not necassary either. A single global
- function is all that is needed, to compare two abstract
- classes of the same type. Such comparison must call on
- the virtual accessor functions to obtain the state of the
- two objects for the comparison.
-
- If the state cannot be accessed this way,
- you have already broken principles of good design.
-
- In particular, it is a complete fallacy that
- because two derived classes have a different type
- they should compare not equal. Derived classes are
- best used to implement abstractions. Two distinct
- derived classes can easily implement the SAME subtype
- of the base abstraction.
-
- For example:
-
- struct Rectangle {
- virtual double getLength()const =0;
- virtual double getBreadth()const =0;
- };
-
- bool operator == (Rectangle const &a, Rectangle const &b) {
- return
- a.getLength() == b.getLength() &&
- a.getBreadth() == b.getBreadth();
- }
-
- Note this works NO MATTER WHAT IMPLEMENTATION OF Rectangle is
- used. For example it will correctly compare "ConcreteSquare"
- with "ConcreteRectangle".
-
- --
- John Max Skaller voice: 61-2-566-2189
- 81 Glebe Point Rd fax: 61-2-660-0850
- GLEBE NSW 2037 web: http://www.maxtal.com.au/~skaller/
- AUSTRALIA email: skaller@maxtal.com.au
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-
- --
- / Kamil Iskra - AMIGA 1200, 68030 50MHz, HDD 850 MB, 10 MB RAM \
- | iskra@student.uci.agh.edu.pl kiskra@ernie.icslab.agh.edu.pl |
- | http://student.uci.agh.edu.pl/~iskra |
- \ PGP public key available via Finger or WWW /
-