I always thought that one of the features of C++ is that variables can
be declared ANYWHERE (such as near where you're going to use them).
However I ran across a situation in MSVC 1.6 with a CPP module that
seems to contradict this. Can someone explain to me why?
If I write a switch statement thusly, I get compiler errors:
switch (foo) {
case 1 :
int bar = 2;
break;
case 2 :
int bar = 3;
break;
}
However, if I write the code this way there are no errors:
switch (foo) {
case 1 : {
int bar = 2;
break;
}
case 2 : {
int bar = 3;
break;
}
}
I was under the assumption that the braces weren't necessary in C++ as
far as declaring variables is concerned. Why then do I need the
braces? Do the added braces affect portability in any way?
* KWQ/2 1.2i *
--- TMail v1.31.5
---------------
* Origin: Diablo Valley PCUG-BBS, Walnut Creek, CA 510/943-6238 (1:161/55)
|