6.7 Nested TypesYou 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.
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( ); } |