RAD Studio |  |

Nested Types
See Also
Collapse All

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,

Copy Code

    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 CodeGear C++ 2.0, any tags or typedef names declared inside a class actually belong to the global (file) scope. For example:

Copy Code

    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:

Copy Code

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

See Also

C++ Classes

Class Types

Class Scope

Class Objects

Class Member List

Member Functions

The Keyword This

Static Members

Inline Functions

Member Scope

Nested Types

Member Access Control

Base And Derived Class Access

Virtual Base Classes

Friends Of Classes

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!