home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / cplus / 1133 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.8 KB  |  95 lines

  1. Path: sparky!uunet!wupost!micro-heart-of-gold.mit.edu!bloom-beacon!eru.mt.luth.se!lunic!sunic!seunet!diab!dalle
  2. From: dalle@diab.se (Jan-Erik Dahlin)
  3. Newsgroups: comp.std.c++
  4. Subject: Incomplete types and nested classes
  5. Message-ID: <DALLE.92Sep7140132@mu.diab.se>
  6. Date: 7 Sep 92 19:01:32 GMT
  7. Sender: news@diab.se
  8. Distribution: comp
  9. Organization: Diab Data AB
  10. Lines: 83
  11.  
  12. steve@taumet.com (Steve Clamage) recently wrote about forward declaration
  13. of nested classes:
  14.  
  15. > You do it the same way you handle any other forward declaration.
  16. > You have to be careful about scopes.
  17. >     class A {
  18. >       public:
  19. >         class C;    // forward declaration of A::C
  20. >         class B {
  21. >         C *p;    // pointer to A::C
  22. >         };
  23. >         class C {
  24. >         B *p;    // pointer to A::B
  25. >         };
  26. >     };
  27.  
  28. How about incomplete types and nested classes? What happens if we skip
  29. the first declaration of class C? I ran into the problem when compiling
  30. some old C code.
  31.  
  32. Section 9.1, paragraph 2 of the draft says: "A class declaration
  33. introduces the class name into the scope where it is declared and hides
  34. any class, object, function or other declaration of that name in an
  35. enclosing scope."
  36.  
  37. Paragraph 3: "An elaborated-type-specifier can also be used in the
  38. declarations of objects and functions. It differs from a class
  39. declaration in that if a class of the elaborated name is in scope the
  40. elaborated name will refer to it." 
  41.  
  42. What happens if no class with that name is found in scope???
  43.  
  44. Paragraph 4 has the following example: "A name declaration takes effect
  45. immediately after the identifier is seen. For example,
  46.  
  47.     class A * A;
  48.  
  49. first specifies A to be the name of a class and the redefines it as
  50. the name of a pointer to an object of that class."
  51.  
  52. I guess this means that class A is introduced into the current scope if
  53. not defined earlier.
  54.  
  55. Example:
  56.  
  57.      class A {
  58.        public:
  59.          class B *bp; // pointer to A::B or B?
  60.         f(void);
  61.      };
  62.  
  63.      class B {
  64.       public:
  65.          int memb;
  66.      };
  67.  
  68.     A::f()
  69.     {
  70.         bp->memb = 4711;    // Is this legal?
  71.     }
  72.  
  73. When the elaborated class specifier for B is seen, should the name be
  74. introduced in the scope of class A (as in a nested class declaration)
  75. or the scope enclosing A?
  76.  
  77. I think that the declaration 'class B *bp;' should introduce (a nested)
  78. class B in the scope of A. The declaration of class B will introduce a
  79. new type in the file/global scope. When 'bp' is used in A::f() it is a
  80. pointer to the local class B in A's scope, which still is incomplete.
  81. Since both Cfront and gcc accept the above declarations without complaint
  82. I assume I've misinterpreted the standard. Could someone, please,
  83. straighten things out for me?
  84.  
  85. > Steve Clamage, TauMetric Corp, steve@taumet.com
  86. > Vice Chair, ANSI C++ Committee, X3J16
  87.  
  88.                             /Jan-Erik Dahlin
  89. --
  90. "Sometimes the magic works and sometimes it doesn't."
  91.  Chief Dan George of the Sioux Indian tribe, after an unsuccessful rain dance.
  92.