home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!caen!deccrl!bloom-beacon!eru.mt.luth.se!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: References as class members
- Message-ID: <4831@holden.lulea.trab.se>
- Date: 17 Aug 92 18:19:30 GMT
- References: <LVI.92Aug14161521@aste27.ida.liu.se>
- Distribution: comp
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 49
- X-Newsreader: Tin 1.1 PL4
-
- lvi@ida.liu.se (Lars Viklund) writes:
- :
- : Is the following class declaration legal?
- :
- : class X {
- : X& r;
- : };
-
- Two relevant (and contradicting!) quotes from the ARM:
-
- (p291, about initializers)
-
- "This is the only way to initialize nonstatic const and
- reference members.",
- which implies that reference members must be initialized
- in conjuction with a constructor.
-
- (p151, about aggregate initialization)
-
- "An aggregate is an array or an object of a class with no
- constructors, no private or protected members, no base classes,
- and no virtual functions".
-
- G++ (version 1.40.3) seems to give p151 precedence, whereas CFront
- takes p291 literally.
-
- With g++ you must explicitly initialize the "r" member, like this
- X x = { x };
-
- Similarly, CFront and g++ disagrees on the legality of the following:
- struct S { const int i; } s = { 3 };
-
- The g++ interpretation may seem reasonable, but it does contradict
- a third quote from the ARM (p264):
-
- "A default constructor will be generated [only] if no constructor
- has been declared for class X",
-
- which in this case is impossible to do for class X (there is no
- reasonable default value for member "r").
-
-
- So I guess the answer is "no", after all.
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-