[comp.lang.c++] C++ Nested Classes

whisd@sersun1.essex.ac.uk (Whiteside S D B) (06/06/91)

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.

   My main worry here is that this implies that the nested class
   name needs to be unique throughout the whole name space.

ii)If I declare an unnamed class, that's acceptable. But then I
   can't access the class definition to define my member functions!

   Without the naming I can't get my scope modifier :: to work, but
   with it, my nested class joins the big bad global world of classes,
   with no protection!

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?

Comments please.

Simon Whiteside

steve@taumet.com (Stephen Clamage) (06/06/91)

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

mnm@hpcupt3.cup.hp.com (Michey Mehta) (06/06/91)

/ hpcupt3:comp.lang.c++ / whisd@sersun1.essex.ac.uk (Whiteside S D B) / 10:27 am  Jun  5, 1991 /


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.

   My main worry here is that this implies that the nested class
   name needs to be unique throughout the whole name space.

ii)If I declare an unnamed class, that's acceptable. But then I
   can't access the class definition to define my member functions!

   Without the naming I can't get my scope modifier :: to work, but
   with it, my nested class joins the big bad global world of classes,
   with no protection!

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?

Comments please.

Simon Whiteside
----------