By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}
};
Sep 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Sep 22 '06 #2

P: n/a
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...
Sep 22 '06 #3

P: n/a
"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
Sep 24 '06 #4

P: n/a
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.8):

"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
Sep 24 '06 #5

P: n/a
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.8):

"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.
Sep 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.