Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is nested class automatically friend of class that it is nested in?

Reply
Thread Tools

Is nested class automatically friend of class that it is nested in?

 
 
request@no_spam.com
Guest
Posts: n/a
 
      09-22-2006
I have a little piece of code that compiles fine but I think it shouldn't
compile fine. Here it is:

class outer_class {
public:
outer_class () {}

int operator () (int i1, int i2) {
return i1+i2;
}

class inner_class {
public:
inner_class () {}

void inner_method (bool b) {
int retv;
outer_class outer_obj;

if (b) {
retv = outer_obj(3,5);
} else {
/* This here shouldn't be allowed, inner class
is not made friend of outer class anywhere! */
retv = outer_obj(3,5,true);
}
}
};

private:
int operator () (int i1, int i2, bool ignored) {
return i1 * i2;
}
};
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      09-22-2006
request@no_spam.com wrote:
> I have a little piece of code that compiles fine but I think it
> shouldn't compile fine. Here it is:
>
> class outer_class {
> public:
> outer_class () {}
>
> int operator () (int i1, int i2) {
> return i1+i2;
> }
>
> class inner_class {
> public:
> inner_class () {}
>
> void inner_method (bool b) {
> int retv;
> outer_class outer_obj;
>
> if (b) {
> retv = outer_obj(3,5);
> } else {
> /* This here shouldn't be allowed, inner class
> is not made friend of outer class anywhere! */
> retv = outer_obj(3,5,true);
> }
> }
> };
>
> private:
> int operator () (int i1, int i2, bool ignored) {
> return i1 * i2;
> }
> };


It is a member of outer_class, so it should have the same access
rights as all the other members, like the member functions.


Bo Persson


 
Reply With Quote
 
 
 
 
request@no_spam.com
Guest
Posts: n/a
 
      09-22-2006
On Fri, 22 Sep 2006 18:03:23 +0200, Bo Persson wrote:

> request@no_spam.com wrote:
>> I have a little piece of code that compiles fine but I think it
>> shouldn't compile fine. Here it is:
>>
>> class outer_class {
>> public:
>> outer_class () {}
>>
>> int operator () (int i1, int i2) {
>> return i1+i2;
>> }
>>
>> class inner_class {
>> public:
>> inner_class () {}
>>
>> void inner_method (bool b) {
>> int retv;
>> outer_class outer_obj;
>>
>> if (b) {
>> retv = outer_obj(3,5);
>> } else {
>> /* This here shouldn't be allowed, inner class
>> is not made friend of outer class anywhere! */
>> retv = outer_obj(3,5,true);
>> }
>> }
>> };
>>
>> private:
>> int operator () (int i1, int i2, bool ignored) {
>> return i1 * i2;
>> }
>> };

>
> It is a member of outer_class, so it should have the same access
> rights as all the other members, like the member functions.
>
> Bo Persson


That makes sense, thank you...
 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      09-24-2006
"Bo Persson"
> It is a member of outer_class, so it should have the same access
> rights as all the other members, like the member functions.



Can someone confirm these 2 points? Can a class, which is a type, be
considered a member? I think that it is only proposed that a nested
class has access to the nesting classes privates. It doesn't work with
my compiler.

Fraser.



Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      09-24-2006
Fraser Ross wrote:
> "Bo Persson"
>> It is a member of outer_class, so it should have the same access
>> rights as all the other members, like the member functions.

>
>
> Can someone confirm these 2 points? Can a class, which is a type,
> be
> considered a member? I think that it is only proposed that a nested
> class has access to the nesting classes privates.


You are correct, it is a proposal for the next standard. I thought it
had passed already.

The current standard says (11.:

"The members of a nested class have no special access to members of an
enclosing class".


The draft for the next standard says the opposite:

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


> It doesn't work with my compiler.


It does work with my compiler!


Bo Persson


 
Reply With Quote
 
T.A.
Guest
Posts: n/a
 
      09-25-2006
On Mon, 25 Sep 2006 00:03:07 +0200, Bo Persson wrote:

> Fraser Ross wrote:
>> "Bo Persson"
>>> It is a member of outer_class, so it should have the same access
>>> rights as all the other members, like the member functions.

>>
>>
>> Can someone confirm these 2 points? Can a class, which is a type,
>> be
>> considered a member? I think that it is only proposed that a nested
>> class has access to the nesting classes privates.

>
> You are correct, it is a proposal for the next standard. I thought it
> had passed already.
>
> The current standard says (11.:
>
> "The members of a nested class have no special access to members of an
> enclosing class".
>
> The draft for the next standard says the opposite:
>
> "A nested class is a member and as such has the same access rights as
> any other member."
>
>> It doesn't work with my compiler.

>
> It does work with my compiler!
>
> Bo Persson


Yeah, it also works with mine, but I'm suspicious on many things that work
with that particulyr compiler... For example if aou leave some setting
default following code comiples fine:

for (int i = 0; i < 10; ++i) {
...
}
....
i = 12;
....

(The scope of i is not forced to for loop by default)

Now, this is just an example...Compilers should not be trusted when
checking is some bahvior standard or not.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
Friend brings a friend. Shisha Girl MCSE 4 03-03-2006 02:42 AM
Friend brings a friend. Shisha Girl Python 0 03-02-2006 02:28 AM
friend sibling class nested in derived class problem in gcc 4.0 Tomas Sieger C++ 1 12-20-2005 08:49 AM



Advertisments