On Nov 5, 3:06 pm, zade <zhaohongc...@gmail.com> wrote:
> I've read some code like below:
> class Foo
> int foo;
> public :
> Foo(int foo)
> I try it in VC2008, and it is fine.
> My question is :in the ctor, the name "foo" here does not cause any
> ambiguity, why that? Is this a standard way or just a workaround ?
It's standard-compliant. Whether it's a good idea is up to your taste.
C++03 12.6.2p1 gives this production for initializer lists:
mem-initializer , mem-initializer-list
mem-initializer-id ( expression-list[opt] )
12.6.2p2 then says:
Names in a mem-initializer-id are looked up in the scope of the
constructor's class and,
if not found in that scope, are looked up in the scope containing
Note in particular that names are not looked up within the scope of
the constructor itself, so the parameters are not found.
The expression list itself follows normal name lookup rules, which
means that the parameters hide the member variables.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]