Freigeben über


Nested Type Names

Microsoft C++ supports declaration of nested types — both named and anonymous. Anonymous structs and classes are a Microsoft specific extension to C++. Anonymous unions are standard.

[::] class-name :: class-name
[::] template-id :: class-name
[::] class-name :: template-id
[::] template-id :: template template-id
:: template template-id :: template template-id

Remarks

The first class-name above refers to the outer class; the second refers to the inner class. These names may be further nested. Thus,

Outer::Inner
::Outer::Inner
::Outer::Inner::Inner2

are all valid names of classes. In place of class names, template identifiers may be used, with the optional template keyword, as in

MyClass<int>::Inner
Outer<int>::template Inner<int>

In some programming situations, it makes sense to define nested types. These types are visible only to member functions of the class type in which they are defined. They can also be made visible by constructing a qualified type name using the scope-resolution operator (::).

Hinweis

One commonly used class hierarchy that employs nested types is iostream. In the iostream header files, the definition of class ios includes a series of enumerated types, which are packaged for use only with the iostream library.

Example

The following example defines nested classes:

// nested_type_names1.cpp
class WinSystem
{
public:
   class Window
   {
   public:
      Window();   // Default constructor.
      ~Window();   // Destructor.
      int NumberOf();   // Number of objects of class.        
      int Count();   // Count number of objects of class.
   private:
      static int CCount;
   };

   class CommPort
   {
   public:
      CommPort();   // Default constructor.
      ~CommPort();   // Destructor.
      int NumberOf();   // Number of objects of class.
      int Count();   // Count number of objects of class.
   private:
      static int CCount;
   };
};

// Initialize WinSystem static members.
int WinSystem::Window::CCount = 0;
int WinSystem::CommPort::CCount = 0;

int main()
{
}

To access a name defined in a nested class, use the scope-resolution operator (::) to construct a complete class name. Use of this operator is shown in the initializations of the static members in the preceding example. To use a nested class in your program, use code such as:

WinSystem::Window Desktop;
WinSystem::Window AppWindow;

cout << "Number of active windows: " << Desktop.Count() << "\n";

Nested anonymous classes or structures can be defined as:

// nested_type_names2.cpp
class Ledger
{
   class
   {
   public:
      double PayableAmt;
      unsigned PayableDays;
   } Payables;

   class
   {
   public:
      double RecvableAmt;
      unsigned RecvableDays;
   } Receivables;
};

int main()
{
}

An anonymous class must be an aggregate that has no member functions and no static members.

END Microsoft specific

Hinweis

Although an enumerated type can be defined inside a class declaration, the reverse is not true; class types cannot be defined inside enumeration declarations.

See Also

Concepts

C++ Type Specifiers