473,790 Members | 3,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1540

"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.RE MOVE.co.uk> wrote in message
news:Xn******** *************** **********@195. 129.110.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.ne st 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.RE MOVE.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.ne st 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
8680
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
3143
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 wish that my predecessor had followed) but -- alas -- being a relative beginner to Access, I can't see the reasoning behind one of the points and the site does not provide any rationale / explanation for its presence either: 2. Thou shalt never...
9
2019
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, Phone) tblzips = holds ( Zip codes, City, State, County, Country) tblzips ID Zipcode City State County Country
0
1401
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 reference while there is no reference in signature. Code
1
2086
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 value tables all have the same format: ID, Value, ValidFrom, ValidTo The code I use to get the form to look at the correct lookup value table is: Dim db As Database Dim tdfTable As TableDef
9
2577
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
3558
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: class X( object ): y = 'private value' def get_y( self ): return self.y
1
6791
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 you to look up the next smallest value to what you provided, and return any corresponding field from the table. Very useful for looking up things like currency exchange rates, tax rates, etc., where there might not be an entry for every day/income...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.