Lecture overview -- Keyboard shortcut: 'u'  Previous page: What about interfaces in C++? -- Keyboard shortcut: 'p'  Next page: What about prevention of derivation in C++ -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 4 - Page 10 : 24
Notes about C++
Abstraction Mechanisms, Part 2
What about nested classes in C++?

Classes can be nested in C++

Similar to inner static classes in Java

No consequences for the nesting of instances of the involved classes

  • The C++ Programming Language: Page 851-852
 

  • "The members of a member class have no special access to members of an enclosing class"

    • According to Stroustrup page 851

    • In g++ instances of an inner class can access private members of instances of an outer class

      • This seems to be reasonable because class Inner is nested in class Outer

  • If an instance i of Inner needs access to the instance o of Outer, o must passed as a parameter to the constructor of Inner, or to an Innner method

    y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/nested-classes/nested-1.cppClass Outer that contains class Inner - does not compile.


    y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/nested-classes/nested-2.cppClass Outer that contains class Inner - friends of each other.


    Related examples below ...

    y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/nested-classes/nested-2a.cppA variant where class Inner is private in Outer - does not compile.


    y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/nested-classes/nested-3.cppInner attempts to access to non-static variable in Outer - does not compile.


    y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/nested-classes/nested-3a.cppProblems solved - This program compiles.