home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12446 < prev    next >
Encoding:
Internet Message Format  |  1992-08-17  |  1.9 KB

  1. Path: sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!caen!deccrl!bloom-beacon!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: References as class members
  5. Message-ID: <4831@holden.lulea.trab.se>
  6. Date: 17 Aug 92 18:19:30 GMT
  7. References: <LVI.92Aug14161521@aste27.ida.liu.se>
  8. Distribution: comp
  9. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  10. Lines: 49
  11. X-Newsreader: Tin 1.1 PL4
  12.  
  13. lvi@ida.liu.se (Lars Viklund) writes:
  14. : Is the following class declaration legal?
  15. : class X {
  16. :     X& r;
  17. : };
  18.  
  19. Two relevant (and contradicting!) quotes from the ARM:
  20.  
  21. (p291, about initializers)
  22.  
  23.     "This is the only way to initialize nonstatic const and
  24.     reference members.",
  25.     which implies that reference members must be initialized
  26.     in conjuction with a constructor.
  27.  
  28. (p151, about aggregate initialization)
  29.  
  30.     "An aggregate is an array or an object of a class with no
  31.     constructors, no private or protected members, no base classes,
  32.     and no virtual functions".
  33.  
  34. G++ (version 1.40.3) seems to give p151 precedence, whereas CFront
  35. takes p291 literally.
  36.  
  37. With g++ you must explicitly initialize the "r" member, like this
  38.    X x = { x };
  39.  
  40. Similarly, CFront and g++ disagrees on the legality of the following:
  41.    struct S { const int i; } s = { 3 };
  42.  
  43. The g++ interpretation may seem reasonable, but it does contradict
  44. a third quote from the ARM (p264):
  45.  
  46. "A default constructor will be generated [only] if no constructor
  47.  has been declared for class X",
  48.  
  49. which in this case is impossible to do for class X (there is no
  50. reasonable default value for member "r").
  51.  
  52.  
  53. So I guess the answer is "no", after all.
  54.  
  55. -- 
  56. --------------------------------------------------------------------------
  57. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  58. | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490              |
  59. --------------------------------------------------------------------------
  60.