home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / cplus / 2135 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.5 KB  |  83 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!parc!ellis
  3. From: ellis@parc.xerox.com (John Ellis)
  4. Subject: ARM ambiguity about member initializers and virtual functions
  5. Message-ID: <ellis.728095505@osric>
  6. Sender: news@parc.xerox.com
  7. Organization: Xerox PARC
  8. Date: 27 Jan 93 00:45:05 GMT
  9. Lines: 72
  10.  
  11. For the purposes of binding virtual functions, is a member initializer
  12. considered part of the constructor in which it appears?  The answer
  13. might seem obviously yes, but the ARM appears contradictory on this point.
  14.  
  15. Consider this test program:
  16.  
  17.     #include <iostream.h>
  18.  
  19.     struct A {
  20.         A() {f();}
  21.         virtual int f() {
  22.             cout << "In A::f.\n";
  23.             return 0;}
  24.         int g() {return f();}};
  25.  
  26.     struct B: A {
  27.         B(): i( g() ) {g();}
  28.         int i;
  29.         virtual int f() {
  30.             cout << "In B::f.\n";
  31.             return 0;}};
  32.  
  33.     void main() {B b;}
  34.  
  35. Three compilers we tested believe that member initializers are part of a
  36. constructor as regards virtual functions.  They produced this output:
  37.  
  38.     In A::f.
  39.     In B::f.
  40.     In B::f.
  41.  
  42. whereas a fourth compiler, known for its rigid adherence to the
  43. standard, has a contrary belief:
  44.  
  45.     In A::f.
  46.     In A::f.
  47.     In B::f.
  48.  
  49. The natural interpretation, that a member initializer is part of its
  50. constructor, comes from two passages in the ARM:
  51.  
  52.     Page 294, 12.7: Member functions may be called in constructors and
  53.     destructors.  ... The function called will be the one defined in
  54.     the constructor's (or destructor's) own class or its bases, but
  55.     *not* any function overriding it in a derived class.
  56.     
  57.     Page 294, 12.6.2: A mem-initializer is evaluated in the scope of
  58.     the constructor in which it appears.
  59.  
  60. But some earlier commentary clearly contradicts the natural
  61. interpretation:
  62.  
  63.     Page 233, 10.9c: When B::B() is executing, the C part of the
  64.     object being constructed exists only as raw storage and cannot be
  65.     accessed through a virtual function.  The available f() is A::f().
  66.     The first point at which C::f() can be called as f() is the first
  67.     *statement* in the constructor C::C(). [emphasis added]
  68.  
  69. This clearly states that a class's virtual functions are not available
  70. before the first statement of a constructor.  Since member
  71. initializers are executed before the statements of the constructor:
  72.  
  73.     Page 292, 12.6.2: ...then the members are initialized...then the
  74.     body of D::D() is executed (12.1).
  75.  
  76. this implies that a class's virtual functions are not available to a
  77. member initializer.
  78.  
  79. Is section 10.9c wrong?  
  80.  
  81.  
  82.  
  83.