Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Hi All,

In one of the famous book called Thinking in C++, in one of the example Bruce Eckel made nested class as friend like,

class upper
{
private:
int i;

class nested;// forward declaration  

friend nested;

class nested
{
public:
uppder obj;
void SomeFunction()
{
cout<< obj.i; 
}
};

};


I used above code without using friend to nested class and worked correctly. So what should be the conclusion, does nested class should be declared as friend for accessing private data of outer class or it can directly access it without friend keyword.
Posted

1 solution

A nice question in this section after long time.

Pranit, Bruce Eckel gave this statement as it is true based on the 1998 C++ draft. Now if you are using any compiler say MSVC 6.0 then you will get error if you haven't made your nested class friend class.

However a latter draft corrected this and stated that
"A nested class is a member and as such has the same access rights as any other member",
so if you use any compiler shipped with this modification then the use of 'friend' is no longer needed
 
Share this answer
 
Comments
[no name] 31-Mar-12 2:57am    
Thanks for your nice words and answer. 5!

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