DekGenius.com
Team LiB   Previous Section   Next Section

6.7 Nested Types

You can declare and define types within a class definition. Accessing these nested types is similar to using types declared within a namespace; the class name serves as the namespace name.

Java programmers have several different kinds of nested classes to choose from, but C++ has only one. A C++ nested class is like a static member class in Java. You can construct the other forms of nested classes by adding the appropriate declarations and support code. For example:

class outer {
public:
  friend inner;   // Implicit in Java
  class inner {
  friend outer; // Implicit in Java
  public:
    // Inner member class keeps track of outer instance.
    inner(outer& out) : out_(out) {}
    int get(  ) const { return out_.x(  ); }
  private:
    outer& out_;
  }
  int x(  );
  int y(  ) { inner i(*this); return i.get(  ); }
};

Nested types obey access specifiers, so private types are usable only by the class and its friends; protected types are usable by the class, derived classes, and friends. Public types are usable anywhere. To use a nested type outside the containing class and derived classes, it must be qualified with the class name.

Nested enumerations add every enumerator to the class scope:

class Graphics {
public:
  enum color { black, red, green, yellow, blue, magenta, cyan, white };
  color pixel(int row, int col) const;
  ...
};
Graphics::color background = Graphics::white;

A typedef declaration can also be nested in a class. This technique is often used for traits (see Chapter 8):

template<typename T, std::size_t N>
class array {
public:
  typedef T value_type;
  typedef T* pointer_type;
  typedef T& reference_type;
  typedef std::size_t size_type;
  ...
};

A nested-class definition is like an ordinary class definition, except the name is local to the outer class. The inner class has no special access privileges to the outer class, and vice versa. A nested class can be declared within its outer class definition and later defined at namespace scope. In that case, the nested name must be qualified with the outer class name. Often, nested classes are fully defined within their outer classes:

struct outer {
  struct inner1 {
    int foo(  ) { return 10; }
  };
  class inner2;
};
struct outer::inner2 {
  int foo(  );
};
int outer::inner2::foo(  ) {
  outer::inner2 x;
  return x.foo(  );
}
    Team LiB   Previous Section   Next Section