473,287 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 1514

"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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.