Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: C++ Nested Classes Message-ID: <759@taumet.com> Date: 6 Jun 91 16:31:55 GMT References: <5263@servax0.essex.ac.uk> Organization: Taumetric Corporation, San Diego Lines: 69 whisd@sersun1.essex.ac.uk (Whiteside S D B) writes: >It lets me nest a class within another, but... >i) If the class is named, then I can access the class outside the > enclosing class (or inside it, if I assign a variable to the > class declaration). This seems odd. The nested declaration has > not "hidden" the class name. >ii)If I declare an unnamed class, that's acceptable. But then I > can't access the class definition to define my member functions! >It's hard to see a way around this. It there a theoretical reason why >C++ is set up this way? Is it a remnant of "globalish" C or am I >just confused? You have every right to be confused. This is an area of change in C++. In the original definition of C++, a type defined inside a class was exported, so that class C { class D { ... }; ... }; and class D { ... }; class C { ... }; were equivalent. In E&S (and in what will become the ANSI C++ Standard), the two are not equivalent, and nested type defintions are truly nested. That is, under the newest definitions you will get the effect you want: class D will not be visible except in the scope of class C. I don't know whether any currently-available C++ compilers fully implement this language feature. AT&T's version 2.1 implements a compromise, whereby class D is exported unless there is already a visible class D; then it is nested. Examples: ============================ class C { class D {}; };// class D exported ============================ class D { }; // global class D class C { class D {}; };// class D nested ============================ class D; // shows there is a global class D class C { class D {}; };// class D nested class D { }; // global class D ============================ class C { class D {}; };// class D exported class D { }; // error This is NOT what the language specification says, but as I said, we are in a transition period. I do know that there will soon be a number of commercially-available compilers which implement fully-nested types. -- Steve Clamage, TauMetric Corp, steve@taumet.com