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

R

request

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;
}
};
 
B

Bo Persson

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
 
R

request

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...
 
F

Fraser Ross

"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.
 
B

Bo Persson

Fraser said:
"Bo Persson"


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
 
T

T.A.

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 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top