Search This Blog

Saturday, November 14, 2009

C++, Nested (inner) Class

To define a helper inner class (nested class): P.672, C++primer
  • Define the inner class in the private section(of the outer class), so others can't use it. (except friends)
  • Define the inner class members (data, function) public, so inner class doesn't have to grand friendship to the outer class.
  • In the outer class, grand friendship to the inner class, so the inner class can access the private section of the outer class. (forward declare inner class, and then grand friendship) p852.B.J.
  • You can delay the inner class definition with forward declare it with forward declaration. Now the outer class can use the inner class as pointer or reference. Later, we can define the inner class at the global scope, using namespace to qualify the name.
  • Inner class cannot directly access the outer class public section (this pointer problem), unless the stuff(in outer class) we want to access(from inner class) is static, type name, enumerator, or through pointer or reference of the outer class.

No comments: