Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I was surfing in the INTERNET here and there looking for the concept of the intern class concept in the C++.

Some people say that the gcc allow the inner class accessing the members of the enclosing class whereas the MVS compiler reject it.

So people, which one is correct ?



11.8 Nested classes [class.access.nest]

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 friend-
ship to an enclosing class; the usual access rules (_class.access_)
shall be obeyed. The members of an enclosing class have no special
access to members of a nested class; the usual access rules
(_class.access_) shall be obeyed.
[Example:]
C++
class E {
    int x;
    class B { };

    class I {
        B b;            // error: E::B is private
        int y;
        void f(E* p, int i)
        {
            p->x = i;   // error: E::x is private
        }
    };
    int g(I* p)
    {
        return p->y;    // error: I::y is private
    }
};

[--end example]
Posted
Comments
enhzflep 18-Apr-12 7:46am    
Not sure to be honest. If it's part of the language spec, they both should. Seems unlikely that msvc would differ on something as important as this, though it does allow this (gcc doesn't)

class myClass
{
int myClass::someFunc();
};

GCC requires

class myClass
{
int someFunc();
};


In any case, you should make the class members protected (by default they're private, and thus inaccessible)

However, since int g(I* p) is a member function of class E and it relies on the y member var of the internal class I, it won't work. Class I _can_ access E::x, but to get the I::y from E::g(I *p), I believe you have to include a function inside class I that returns the value of y

try this:

class E
{
protected:
int x;
class B { };

class I
{
public:
int getY()
{
return y;
}
protected:
B b; // error: E::B is private
int y;
void f(E* p, int i)
{
p->x = i; // error: E::x is private
}
};
int g(I* p)
{
return p->getY(); // error: I::y is private
}
};
Resmi Anna 18-Apr-12 7:59am    
This is really new and quite interesting. Actually I was expecting an error after gone through the documents about Nested class in "http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr061.htm"
I complied in MS visual studio. But to my surprise there was no error in the first 2 cases. Only the last one reported an error. C++ concepts should be same for all compilers..rt??Complier Bug??? Really confused.

There really isn't a concept of an internal class in C++ the way there is in Java.

If you declare a nested class it doesn't have any privileged access to members of the enclosing class - it effectively doesn't know the enclosing class is there. The enclosing class doesn't have any access to the members of the nested class - it's either just like another data member (if you declare and instance of it) or another level of namespace scoping.

About the only time you need a nested class in C++ is when you've got two concepts to model and one's an implementation detail of the other. And even then there are better ways of modelling the dependency - i.e. sticking the class you were going to nest in the enclosing class's implementation file.

So if you ever feel the urge to nest classes think again, it really only makes your code harder to read and doesn't change the access of the various bits of your code to each other compared to other methods.

Cheers,

Ash
 
Share this answer
 
C++ concepts should be same for all compilers..rt??

should be, yes. but in reality, compilers often differ on things like this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900