Nested Types

From RAD Studio
Jump to: navigation, search

Go Up to Member Scope Index

Tag or typedef names declared inside a class lexically belong to the scope of that class. Such names can, in general, be accessed only by using the xxx::yyy notation, except when in the scope of the appropriate class.

A class declared within another class is called a nested class. Its name is local to the enclosing class; the nested class is in the scope of the enclosing class. This is a purely lexical nesting. The nested class has no additional privileges in accessing members of the enclosing class (and vice versa).

Classes can be nested in this way to an arbitrary level, up to the limits of memory. Nested classes can be declared inside some class and defined later. For example,

struct outer
   typedef int t;  // 'outer::t' is a typedef name
   struct inner    // 'outer::inner' is a class
      static int x;
   static int x;
      int f();
   class deep;     // nested declaration
int outer::x;     // define static data member
int outer::f() {
   t x;           // 't' visible directly here
   return x;
int outer::inner::x;    // define static data member
outer::t x;             //  have to use 'outer::t' here
class outer::deep { };  // define the nested class here

With C++Builder, any tags or typedef names declared inside a class actually belong to the global (file) scope. For example:

struct foo
   enum bar { x };   // 2.0 rules: 'bar' belongs to file scope
                     // 2.1 rules: 'bar' belongs to 'foo' scope
bar x;

The preceding fragment compiles without errors. But because the code is illegal under the 2.1 rules, a warning is issued as follows:

Warning: Use qualified name to access nested type 'foo::bar'

See Also