C ++ Nested Classes - Internal / External

I read several posts about Nested classes in our community and beyond, and I'm pretty confused.

As far as I understand, in C ++, nested classes are no different from separate / independent classes.

While I was trying to better understand conecpt, I wrote simple code and I found out that the inner class can access the outer class without getting friendship from the outer class.

For example:

class Outer { private : // default access modifier int x; static int sx; public: class Inner { public: void changeOuterDataMemberValues(int value) { sx = value; // changes the private static data member of Outer. Outer out; out.x = value; // changes the private data member via object (not via class!) } void printMyOuterDataMember() { cout << sx; // prints the private data member of Outer. } }; }; class Lonesome { void tryingToChangeDataMemberValue(int value) { Outer::sx = value; // cannot change the private static data member of Outer. } }; int Outer::sx; 

You can see that the Inner class, which is nested in the Outer class, has access to its data members (Outer class), and Lonesome, as an independent class, cannot access the data element of the Outer class.

I apologize if this is a duplicate or a stupid question, but I just want to confirm with you that there is a difference between a Nested class and an independent class (two different classes that have no internal / external relationship).

Thanks everyone, Syndicator =]

+8
c ++
source share
3 answers

In this regard, there is a difference between C ++ 03 and C ++ 11. Thus, the answer depends on which compiler you use.

If you use a compiler compatible with C ++ 03, then:

A nested class cannot access all members of the enclosing class.

If you use a compiler compatible with C ++ 11, then:

A nested class can access all members of the enclosing class. A nested class is considered just another member of the class.

C ++ 03 Standard 11.8 Nested classes:
ยง one

Members of a nested class do not have special access to members of the enclosing class, or to classes or functions that provide friendship to the surrounding class; normal access rules must be followed.

C ++ 11 Standard 11.7 Nested classes:

A nested class is a member and, as such, has the same access rights as any other member.

+21
source share

From section 11.7 of the standard:

1 - A nested class is a member and, as such, has the same access rights as any other member. Members of the closing class do not have special access to members of the nested class [...]

Thus, a nested ("inner") class is actually a friend of the surrounding class.

+3
source share

You yourself answered your question:

You can see that the Inner class, which is nested in the Outer class, has access to its data members (Outer class), and Lonesome, as an independent class, cannot access the data element of the Outer class.

Note the random in Java in C ++, you cannot access non-stationary members of the Outer class.

It is often useful to give a reference to the Outer class to the Inner class.

 class Outer { private : // default access modifier int x; static int sx; public: class Inner { public: Inner( Outer & o ) : m_outer( o ) {} void changeOuterDataMemberValues(int value) { sx = value; // changes the private static data member of Outer. //Outer out; //out.x = value; // changes the private data member via object (not via class!) m_outer.x = value; // <--------- } void printMyOuterDataMember() { cout << sx; // prints the private data member of Outer. cout << m_outer.x; } private: Outer & m_outer; }; }; 
+2
source share

All Articles