To grant nested classes access rights to the private members of other nested
classes, or to grant a surrounding class access to the private members of its
nested classes the hi(friend: nested classes)tt(friend) keyword must be used.

Note that no friend declaration is required to grant a nested class access to
the private members of its surrounding class. After all, a nested class is a
type defined by its surrounding class and as such objects of the nested class
are members of the outer class and thus can access all the outer class's
members. Here is an example showing this principle. The example won't compile
as members of the class tt(Extern) are denied access to tt(Outer)'s private
members, but tt(Outer::Inner)'s members em(can) access tt(Outer)'s private
members:
        verb(
    class Outer
    {
        int d_value;
        static int s_value;

        public:
            Outer()
            :
                d_value(12)
            {}
            class Inner
            {
                public:
                    Inner()
                    {
                        cout << "Outer's static value: " << s_value << '\n';
                    }
                    Inner(Outer &outer)
                    {
                        cout << "Outer's value: " << outer.d_value << '\n';
                    }
            };
    };
    class Extern            // won't compile!
    {
        public:
            Extern(Outer &outer)
            {
                cout << "Outer's value: " << outer.d_value << '\n';
            }

            Extern()
            {
                cout << "Outer's static value: " << Outer::s_value << '\n';
            }
    };

    int Outer::s_value = 123;
    int main()
    {
        Outer outer;
        Outer::Inner in1;
        Outer::Inner in2{ outer };
    }
            )

Now consider the situation where a class tt(Surround) has two nested
classes tt(FirstWithin) and tt(SecondWithin). Each of the three classes has a
static data member tt(int s_variable):
        verb(
    class Surround
    {
        static int s_variable;
        public:
            class FirstWithin
            {
                static int s_variable;
                public:
                    int value();
            };
            int value();
        private:
            class SecondWithin
            {
                static int s_variable;
                public:
                    int value();
            };
    };
        )
    If the class tt(Surround) should be able to access tt(FirstWithin) and
tt(SecondWithin)'s private members, these latter two classes must declare
tt(Surround) to be their friend. The function tt(Surround::value) can
thereupon access the private members of its nested classes. For example (note
the tt(friend) declarations in the two nested classes):
        verb(
    class Surround
    {
        static int s_variable;
        public:
            class FirstWithin
            {
                friend class Surround;
                static int s_variable;
                public:
                    int value();
            };
            int value();
        private:
            class SecondWithin
            {
                friend class Surround;
                static int s_variable;
                public:
                    int value();
            };
    };
    inline int Surround::FirstWithin::value()
    {
        FirstWithin::s_variable = SecondWithin::s_variable;
        return (s_variable);
    }
        )
    Friend declarations may be provided em(beyond) the definition of the
entity that is to be considered a friend. So a class can be declared a friend
em(beyond) its definition. In that situation in-class code may already use the
fact that it is going to be declared a friend by the upcoming class. As an
example, consider an in-class implementation of the function
tt(Surround::FirstWithin::value). The required tt(friend) declaration can also
be inserted em(after) the implementation of the function tt(value):
        verb(
    class Surround
    {
        public:
            class FirstWithin
            {
                static int s_variable;
                public:
                    int value();
                    {
                        FirstWithin::s_variable = SecondWithin::s_variable;
                        return s_variable;
                    }
                friend class Surround;
            };
        private:
            class SecondWithin
            {
                friend class Surround;
                static int s_variable;
            };
    };
        )


    Note that members named identically in outer and inner classes
(e.g., `tt(s_variable)')
  hi(nested class: member access)
    may be accessed using the proper scope resolution expressions, as
illustrated below:
        verb(
    class Surround
    {
        static int s_variable;
        public:
            class FirstWithin
            {
                friend class Surround;
                static int s_variable;  // identically named
                public:
                    int value();
            };
            int value();

        private:
            class SecondWithin
            {
                friend class Surround;
                static int s_variable;  // identically named
                public:
                    int value();
            };
            static void classMember();
    };
    inline int Surround::value()
    {                                   // scope resolution expression
        FirstWithin::s_variable = SecondWithin::s_variable;
        return s_variable;
    }
    inline int Surround::FirstWithin::value()
    {
        Surround::s_variable = 4;       // scope resolution expressions
        Surround::classMember();
        return s_variable;
    }
    inline int Surround::SecondWithin::value()
    {
        Surround::s_variable = 40;      // scope resolution expression
        return s_variable;
    }
        )

    Nested classes aren't automatically each other's friends. Here tt(friend)
declarations must be provided to grant one nested classes access to another
nested class's private members. 

To grant tt(FirstWithin) access to tt(SecondWithin)'s private members,
tt(SecondWithin) must contain a tt(friend) declaration.

Likewise, the class tt(FirstWithin) simply uses tt(friend class
SecondWithin) to grant tt(SecondWithin) access to tt(FirstWithin)'s private
members. Even though the compiler hasn't seen tt(SecondWithin) yet, a friend
declaration is also considered a forward declaration.

Note that tt(SecondWithin)'s forward declaration cannot be specified inside
tt(FirstWithin) by using `tt(class Surround::SecondWithin;)', as this would
generate an error message like:
        quote(`Surround' does not have a nested type named `SecondWithin')

Now assume that in addition to the nested class tt(SecondWithin) there also
exists an outer-level class tt(SecondWithin). To declare that class a friend
of tt(FirstWithin's) declare tt(friend ::SecondWithin) inside tt(class
FirstWithin). In that case, an outer level class declaration of
tt(FirstWithin) must be provided before the compiler encounters the tt(friend
::SecondWithin) declaration.

Here is an example in which all classes have full access to all private
members of all involved classes: hi(friend: as forward declaration), and a
outer level tt(FirstWithin) has also been declared:
        verb(
    class SecondWithin;

    class Surround
    {
        // class SecondWithin;      not required (but no error either): 
        //                          friend declarations (see below)
        //                          are also forward declarations

        static int s_variable;
        public:
            class FirstWithin
            {
                friend class Surround;
                friend class SecondWithin;
                friend class ::SecondWithin;

                static int s_variable;
                public:
                    int value();
            };
            int value();            // implementation given above
        private:
            class SecondWithin
            {
                friend class Surround;
                friend class FirstWithin;

                static int s_variable;
                public:
                    int value();
            };
    };
    inline int Surround::FirstWithin::value()
    {
        Surround::s_variable = SecondWithin::s_variable;
        return s_variable;
    }
    inline int Surround::SecondWithin::value()
    {
        Surround::s_variable = FirstWithin::s_variable;
        return s_variable;
    }
        )
