Article View: comp.std.c++
Article #262Re: the most dissatisfying part of c++
From: Paul C Brown
Date: Wed, 01 Aug 1990 21:02
Date: Wed, 01 Aug 1990 21:02
58 lines
2019 bytes
2019 bytes
In article <27634@netnews.upenn.edu>, limsoon@saul.cis.upenn.edu
(Limsoon Wong) writes:
|>
|>the program below is a contrived one. it cannot be compiled.
|>the reason is a type fault. however, it exhibits a very central
|>characteristic of update. something should be done to allow
|>this kinds of programs. ---limsoon.
|>
|>
|>
|>#include <stream.h>
|>class a {
|> public:
|> a& test() {
|> // do some modification to the state of this object,
|> // then ...
|> return *this;
|>};
|>class b : public a {
|> public:
|> int b;
|>};
|>main() {
|> b B;
|> B = B.test();
|> // type violation despite the fact that
|> // `B' and `B.test()' are the same object.
|>}
Your proposed program is not "type" correct. The method "test" returns
an object
of type a, not of type b. Objects of type a do not exhibit all of the
properties of type b, and therefore are not valid values for variables
of type b. The
following version of main is type correct:
main() {
b B;
a A;
A = B.test();
};
To accomplish what you were trying to do would require a second method on
class b, also named test, that returns an object of type b. If the original
method on class a does, indeed, simply modify the referenced instance, then
the new method could be as simple as:
b::test() {
a temp;
temp = this.test();
return *this;
};
As you have observed, strong typing does not give you everything for
free!
Paul C. Brown brownpc@crd.ge.com
GE Corporate Research & Development
Schenectady, New York
Technical Details
Message-ID:
<10656@crdgw1.crd.ge.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!rpi!crdgw1!sunne!brownpcReferences:
<27634@netnews.upenn.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!rpi!crdgw1!sunne!brownpc From: brownpc@sunne.crd.ge.com (Paul C Brown) Newsgroups: comp.std.c++ Subject: Re: the most dissatisfying part of c++ Message-ID: <10656@crdgw1.crd.ge.com> Date: 1 Aug 90 21:02:00 GMT References: <27634@netnews.upenn.edu> Sender: news@crdgw1.crd.ge.com Reply-To: brownpc@sunne.crd.ge.com (Paul C Brown) Lines: 58