 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
L.Suresh Guest
|
Posted: Wed Jan 19, 2005 9:21 pm Post subject: Names in Class Scope |
|
|
typedef int c;
enum { i = 1 };
class X {
char v[i]; // error: i refers to ::i, but when reevaluated is X::i
int f() { return sizeof(c); } // OK: X::c
char c;
enum { i = 2 };
};
This is the example in 3.3.6/5
How come the "c" in the function int f() {...}
interpreted as X::c and not an error when the name
introduced by the typedef is also in scope.
Thanks for your time.
--lsu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Sun Jan 23, 2005 2:42 am Post subject: Re: Names in Class Scope |
|
|
L.Suresh wrote:
| Quote: | typedef int c;
enum { i = 1 };
class X {
char v[i]; // error: i refers to ::i, but when reevaluated is X::i
int f() { return sizeof(c); } // OK: X::c
char c;
enum { i = 2 };
};
This is the example in 3.3.6/5
How come the "c" in the function int f() {...}
interpreted as X::c and not an error when the name
introduced by the typedef is also in scope.
|
A name in an enclosed scope hides the same name in an enclosing scope.
Unless you use the :: operator, of course.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
L.Suresh Guest
|
Posted: Sun Jan 23, 2005 10:19 am Post subject: Re: Names in Class Scope |
|
|
Then how come char v[i] is an error? Why doesnt the enum { i = 2} hide
the one in the enclosing scope?
It's because, according to 3.3.6/2
"A name N used in a class S shall refer to the same declaration in its
context and when re-evaluated in the completed scope of S. "
In sizeof(c), does not "c" refer to the typedef, and when reevaluated
refers to the char?
--lsu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jan 24, 2005 10:40 pm Post subject: Re: Names in Class Scope |
|
|
L.Suresh wrote:
| Quote: | Then how come char v[i] is an error? Why doesnt the enum { i =
2} hide the one in the enclosing scope?
It's because, according to 3.3.6/2
"A name N used in a class S shall refer to the same
declaration in its context and when re-evaluated in the
completed scope of S. "
In sizeof(c), does not "c" refer to the typedef, and when
reevaluated refers to the char?
|
Because of §3.3.6/1, point 1: "The potential scope of a name
declared in a class consists not only of the declarative region
following the name's declarator, but also of all function
bodies, default arguments, and constructor ctor-initializers in
that class (including such things as nested classes)." If you
modify your example to read:
typedef int c;
enum { i = 1 };
class X {
char v[sizeof(c)]; // error: c refers to ::ic but when
reevaluated is X::c
int f() { return i; } // OK: X::i
char c;
enum { i = 2 };
};
Within the function, it is as if the function definition
followed the complete class definition. Outside of the
function, the rule you cite applies.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|