Topic: Is this code using casts valid in C++ ?
Author: lamarche@ireq.hydro.qc.ca (Louis Lamarche)
Date: 5 Feb 91 00:39:26 GMT Raw View
class foo { int a; };
class foob : public foo { int b; };
foo afoo;
foo* afooPtr = &afoo;
foob* afoobPtr = (foob*) afooPtr;
foo& afooRef = afoo;
foob& afoobRef = (foob&) afooRef; // Ok with g++1.37.0, rejected by g++1.37.2-
main(){}
This code compiles well with CC 2.0 on a Sun and with Zortech 2.1, but fails
with g++1.37.2-. Is it legal ?
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
| Louis Lamarche, IREQ | lamarche@IREQ.Hydro.Qc.CA
| CP 1000, Varennes | or
| QC, Canada, J3X 1S1 | 514-652-8077 (office) 514-324-2919 (home)
Author: rfg@NCD.COM (Ron Guilmette)
Date: 8 Feb 91 06:07:52 GMT Raw View
In article <5217@s3.ireq.hydro.qc.ca> lamarche@ireq.hydro.qc.ca (Louis Lamarche) writes:
+class foo { int a; };
+class foob : public foo { int b; };
+foo afoo;
+foo* afooPtr = &afoo;
+foob* afoobPtr = (foob*) afooPtr;
+foo& afooRef = afoo;
+foob& afoobRef = (foob&) afooRef; // Ok with g++1.37.0, rejected by g++1.37.2-
+main(){}
+
+This code compiles well with CC 2.0 on a Sun and with Zortech 2.1, but fails
+with g++1.37.2-. Is it legal ?
No. That's illegal.
In your example, a `foob' is a `foo' but not vise-versa. Thus, you may
convert a `foob&' to a `foo&' but not vise-versa.
--
// Ron Guilmette - C++ Entomologist
// Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg
// Motto: If it sticks, force it. If it breaks, it needed replacing anyway.