TITLE: forward reference to nested class (Newsgroups: comp.lang.c++.moderated, 18 Sep 96) GRAHAM: gfm@werple.mira.net.au (Graham Menhennitt) >>> Can anybody tell me whether it is legal to have a forward >>> reference to a nested class where the enclosing class is only >>> known via a forward reference also i.e. is the following >>> legal: >> >>> struct class A; >>> struct A::B; OLIVA: Alexandre Oliva >>Nope. MILLOUR: Christian Millour >any rationale available ? CLAMAGE: clamage@taumet.Eng.Sun.COM (Steve Clamage) The one you supplied below isn't bad. MILLOUR: >.... It means that the full declaration of A must be >seen before parsing the declaration for C::D, which is what forward decl. >are meant to avoid, since we are only declaring a *pointer* to an A::B. > >I understand however that there might be access control issues : what if >B was a private nested struct of A ? Should the compiler report an error >and when ? How could the error be detected ? CLAMAGE: Yes. However, the restriction is just a special case of a more basic rule: You cannot mention a member of a class until the class definition has been seen. Otherwise, you could in principle violate the One Definition Rule without ever modifying the original class definition. The reason forward declarations are part of C and C++ is to allow declarations of strutures which reference one another. In C, programmers noticed that in many cases they could avoid #including header files by supplying forward declarations for structs when only pointers were used. I suppose some came to believe that the purpose of the rule was to provide source-language support for shorter compile times. C++ implementations can and do provide ways to shorten compile times even when you include many header files multiple times. The forward declaration was not intended specifically for that purpose. It was intended to allow creation of data structures which would otherwise be impossible.