472,364 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

access and name lookup, surprised by this code

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
9 1435

"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

"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

"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

"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

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
30
by: Andante.in.Blue | last post by:
I just browsed through some of my Access links when I came across the Ten Commandments of Access (http://www.mvps.org/access/tencommandments.htm). Some of the points I heartily agree with (and...
9
by: Mike McGee | last post by:
I am new to database apps, but I am making a db with access 2002. Here is what I have and what I would like for it to do. tblCustomers = holds customer info (Name, Address, City, State, Zip,...
0
by: ketan | last post by:
Hi, While understanding name lookup using item 32 of EC++ ( Sutter ), i found error from gcc bit unclear. It will be great if anyone can help me. In short - compiler complains about...
1
by: munrrob | last post by:
MS Access 2002 SP 3 I have two forms, one with a combo box looking at available lookup tables and the other an unbound form which will be used by the user to update lookup values. The lookup...
9
by: Michael M. | last post by:
Hi all, I would like to know how to access the NT/2000/XP/2003 Name cache; what I mean by this is: Open a Command Prompt and.., C:\> C:\>IPCONFIG /DISPLAYDNS
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
1
by: scubasteve | last post by:
Looking up values from an Access table is simple. Simulating the 'Range Lookup' functionality from Excel's VLookup formula is a bit trickier. For those that aren't familiar with this, it allows...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.