What C++ Standard about this?

D

dh

Hi all,

int a = 1;

for(int a = 12; ...)
{
....
}

// Does value of a here have anything to do with "int a = 1;" scope, or the
"for" scope?

Thanks,
DH
 
S

Shan

Does value of a here have anything to do with "int a = 1;" scope, or
the
"for" scope?

The "a" in the for loop is in the scope of for loop alone and will not
have any effect on "a" before for loop.

Regards
Shan
 
M

Mike Hewson

dh said:
Hi all,

int a = 1;

for(int a = 12; ...)
{
....
}

// Does value of a here have anything to do with "int a = 1;" scope, or the
"for" scope?

The second 'a' has scope from it's declaration until the end of the for
loop, it 'hides' the first 'a' meantime. The first 'a' will be used ( if
at all ) either up until the for and/or after the closing brace of the
for. ( NB. It's legal but I think confusing practise to do this ).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top