8

I read some posts about Nested Classes in our community and outside and I'm pretty confused.

As far as I understand, in C++, Nested Classes aren't not any different from separate/independent classes.

While I was trying to understand the conecpt better I wrote a simple code and I found out that an inner class can access an outer class without being given 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(the Outer class) data members whilst the Lonesome as independent class cannot access the Outer class data member.

I apologize if this is a duplicate or stupid question, but I just want to confirm with you guys that there is a difference between a Nested Class and independent class (two different classes which don't have inner / outer relationship).

Thank you all, Syndicator =]

SyndicatorBBB
  • 1,757
  • 2
  • 26
  • 44
  • You had answered your question by yourself: "the Inner class ... has access to the Outer class data members whilst the Lonesome ... cannot access the Outer class data member" – borisbn Jan 30 '13 at 15:18
  • Duplicate: http://stackoverflow.com/questions/486099/can-inner-classes-access-private-variables – Mihai Todor Jan 30 '13 at 15:19
  • No not really a duplicate... in http://stackoverflow.com/questions/486099/can-inner-classes-access-private-variables you do not have a "out.x", which is the reason for the problem. Instead here the problem is really if using C++03 or C++11 – Ingo Blackman Jul 07 '13 at 19:48

3 Answers3

22

There is a difference between C++03 and C++11 in this regards. So the answer varies depending on which compiler you are using.

If you are using a C++03 compliant compiler then:

Nested class cannot access all members of the enclosing class.

If you are using C++11 compliant compiler then:

Nested class can access all members of the enclosing class. Nested class is treated as just another member of the class.

C++03 Standard 11.8 Nested classes:
§1

The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules shall be obeyed.

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • +1 : That was exactly what I was looking for... (because I was wondering if this might be compiler dependent; so now I know it is, because it depends which standard version the compiler follows...) – Ingo Blackman Jul 07 '13 at 19:54
  • @IngoBlackman it's up to you to tell the compiler what standard to use – Ryan Haining Nov 09 '14 at 17:57
  • @Ryan Haining: I use a compiler which does not support C++11; but it still allows a nested class to access members of the enclosing class. So yes it is completely compiler dependent... – Ingo Blackman Nov 27 '14 at 19:15
  • @IngoBlackman it depends on the standard you are using, if your compiler doesn't support a given standard then yeah that's a limitation of the compiler, but to say it's "compiler dependent" implies something is non-standard or an extension. If you're using these extensions then you're not actually programming in C++ anymore. – Ryan Haining Nov 28 '14 at 04:40
  • @Ryan Haining: "to say it's "compiler dependent" implies something is non-standard". No: It just points out the pretty obvious reality that compilers and in particular C++ compilers do not follow the standard to the letter; which is not really surprising considering how unbelievable complicated the different C/C++ standards are. – Ingo Blackman Dec 19 '14 at 11:49
  • @IngoBlackman I don't see the distinction. Mixing different C++ standards would result in non-standard compilation – Ryan Haining Dec 19 '14 at 17:05
3

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. The members of an enclosing class have no special access to members of a nested class [...]

So a nested ("inner") class is effectively a friend of the enclosing class.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • That is, if we were to declare the Inner class as a friend of the Outer class It wouldn't make any difference because It is built in? – SyndicatorBBB Jan 30 '13 at 15:18
  • 1
    Correct, nested classes already have access to everything that friends can access. – ecatmur Jan 30 '13 at 15:22
  • 1
    @SyndicatorBBB: Note that this depends on the compiler you are using. This is true only for a compiler complaint with C++11 not C++03 compliant compilers. – Alok Save Jan 30 '13 at 15:27
2

You had answered your question by yourself:

You can see that the Inner class which is nested in the Outer class has access to its(the Outer class) data members whilst the Lonesome as independent class cannot access the Outer class data member.

Pay attention to contrarily to Java in C++ you can not acces to non-static members of Outer class.

It's an often practice to give a reference to Outer class to 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;
    };
};
borisbn
  • 4,988
  • 25
  • 42