access and name lookup, surprised by this code

J

John Harrison

Both gcc 3.3.1 and VC++ 7.1 compile the following code.

struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};

const int Outer::c = 123;

int main()
{
Outer::Inner o;
std::cout << o.f() << '\n';
}

Obviously I'm wrong but I thought the above breaks two different rules of
C++. Firstly isn't c private, secondly how can Inner access a member of
Outer without qualifying the name (i.e. return Outer::c)?

Explanations and references to the standard would be appreciated.

john
 
S

Sharad Kala

John Harrison said:
Both gcc 3.3.1 and VC++ 7.1 compile the following code.

struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};

const int Outer::c = 123;

int main()
{
Outer::Inner o;
std::cout << o.f() << '\n';
}

Obviously I'm wrong but I thought the above breaks two different rules of
C++. Firstly isn't c private, secondly how can Inner access a member of
Outer without qualifying the name (i.e. return Outer::c)?

Explanations and references to the standard would be appreciated.

Am perplexed about your 1st question.
But as for your second question -
Section 9.7/1

"Except by using explicit pointers, references, and object names, declarations
in a nested class can use only type names, static members, and enumerators from
the enclosing class". [Example:
int x;
int y;

class enclose {
public:
int x;
static int s;

class inner {

void f(int i)
{
int a = sizeof(x); // error: refers to enclose::x
x = i; // error: assign to enclose::x
s = i; // OK: assign to enclose::s
::x = i; // OK: assign to global x
y = i; // OK: assign to global y
}

void g(enclose* p, int i)
{
p->x = i; // OK: assign to enclose::x
}

};
};
 
J

John Harrison

Sharad Kala said:
Isn't it just simply a definition for the static member :) ?

I thought there was a rule to the effect that inner classes have no special
access to their enclosing class. So Inner should be declared a friend of
Outer in order to access Outer::c, at least that's what I thought.

john
 
S

Sharad Kala

John Harrison said:
I thought there was a rule to the effect that inner classes have no special
access to their enclosing class. So Inner should be declared a friend of
Outer in order to access Outer::c, at least that's what I thought.

oh ok...I was not very sure what you meant in your first doubt.
 
C

Chris \( Val \)

|
| | >
| > | > >
| > > | > > > Both gcc 3.3.1 and VC++ 7.1 compile the following code.
| > > >
| > > > struct Outer
| > > > {
| > > > struct Inner
| > > > {
| > > > int f() { return c; }
| > > > };
| > > > private:
| > > > static const int c;
| > > > };

[snip]

private:
static const int c = 123;
};

It is also quite legal to fully define an 'const
static integral data member' inside the class itself.

Cheers.
Chris Val
 
R

Rob Williscroft

John Harrison wrote in
I thought there was a rule to the effect that inner classes have no
special access to their enclosing class. So Inner should be declared a
friend of Outer in order to access Outer::c, at least that's what I
thought.

This is from memory (I have no references), but I belive there is no
Standard way of granting friendship to the inner class (*), so the
Standard will be changed so that inner classes are always friends of
there outer (enclosing) class. It maybe the both the compilers are
ahead of the game here (or it could be they have bugs).

(*) Some compilers do allow this, but they are incorrect.

Rob.
 
J

John Harrison

Rob Williscroft said:
John Harrison wrote in


This is from memory (I have no references), but I belive there is no
Standard way of granting friendship to the inner class (*), so the
Standard will be changed so that inner classes are always friends of
there outer (enclosing) class. It maybe the both the compilers are
ahead of the game here (or it could be they have bugs).

(*) Some compilers do allow this, but they are incorrect.

Rob.

Defect 77 modifies the definition of friend for the reasons you describe.
But it doesn't say anything about inner classes always being friends

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#77

Anyone have the definitive reference?

john
 
R

Rob Williscroft

John Harrison wrote in
Defect 77 modifies the definition of friend for the reasons you
describe. But it doesn't say anything about inner classes always being
friends

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#77

Anyone have the definitive reference?

I found this by scrolling down 2 or so pages:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#45

A further page down its says:
<quote>

In 11.8 class.access.nest paragraph 1, change

The members of a nested class have no special access to members of an
enclosing class, nor to classes or functions that have granted
friendship to an enclosing class; the usual access rules (clause 11
class.access) shall be obeyed. to

A nested class is a member and as such has the same access rights as any
other member. </quote>

The DR has status "WP" which IIUC means its being considerd for the
next Standard.

Rob.
 
J

John Harrison

Rob Williscroft said:
John Harrison wrote in


I found this by scrolling down 2 or so pages:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#45

A further page down its says:
<quote>

In 11.8 class.access.nest paragraph 1, change

The members of a nested class have no special access to members of an
enclosing class, nor to classes or functions that have granted
friendship to an enclosing class; the usual access rules (clause 11
class.access) shall be obeyed. to

A nested class is a member and as such has the same access rights as any
other member. </quote>

The DR has status "WP" which IIUC means its being considerd for the
next Standard.

OK thanks, I think I missed that one because its title is access to nested
classes not access from nested classes.

john
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top