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

access and name lookup, surprised by this code

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
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
}

};
};

Jul 22 '05 #2

P: n/a

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
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.


Isn't it just simply a definition for the static member :-) ?
Jul 22 '05 #3

P: n/a

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
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.


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
Jul 22 '05 #4

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
> 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.


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.


oh ok...I was not very sure what you meant in your first doubt.
Jul 22 '05 #5

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
|
| "Sharad Kala" <no*****************@yahoo.com> wrote in message
| news:c1*************@ID-221354.news.uni-berlin.de...
| >
| > "Sharad Kala" <no*****************@yahoo.com> wrote in message
| > news:c1*************@ID-221354.news.uni-berlin.de...
| > >
| > > "John Harrison" <jo*************@hotmail.com> wrote in message
| > > news:c1*************@ID-196037.news.uni-berlin.de...
| > > > 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

Jul 22 '05 #6

P: n/a
John Harrison wrote in
news:c1*************@ID-196037.news.uni-berlin.de:

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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7

P: n/a

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn*********************************@195.129.1 10.205...
John Harrison wrote in
news:c1*************@ID-196037.news.uni-berlin.de:

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.
--
http://www.victim-prime.dsl.pipex.com/


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/wg2...efects.html#77

Anyone have the definitive reference?

john
Jul 22 '05 #8

P: n/a
John Harrison wrote in
news:c1*************@ID-196037.news.uni-berlin.de:
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.
--
http://www.victim-prime.dsl.pipex.com/


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/wg2...efects.html#77

Anyone have the definitive reference?


I found this by scrolling down 2 or so pages:

http://anubis.dkuug.dk/jtc1/sc22/wg2...efects.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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

P: n/a

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.131...
John Harrison wrote in
news:c1*************@ID-196037.news.uni-berlin.de:
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.
--
http://www.victim-prime.dsl.pipex.com/


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/wg2...efects.html#77

Anyone have the definitive reference?


I found this by scrolling down 2 or so pages:

http://anubis.dkuug.dk/jtc1/sc22/wg2...efects.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
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.