Variable Scope
-
Hi everyone! Revising some code I've found this:
for(int i=0; i That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared into the "for", how is possible to use it later? The code compile right, so Was I wrong thinking that "i" _is_declared_ into the "for"? Then, talking with the partners at work someone pointed out that in VS you can do it, but in C++Builder it wouldn't compile. The question is What is the standard? And how are declared that kinds of variables? And just testing I can do things like `for(int i=0; i<5; i++) { ... } i = 6; for(int i=7; i<8; i++) { ... }` :omg::wtf: what is the scope then? :doh: Thank you in advance. Regards.
-
Hi everyone! Revising some code I've found this:
for(int i=0; i That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared into the "for", how is possible to use it later? The code compile right, so Was I wrong thinking that "i" _is_declared_ into the "for"? Then, talking with the partners at work someone pointed out that in VS you can do it, but in C++Builder it wouldn't compile. The question is What is the standard? And how are declared that kinds of variables? And just testing I can do things like `for(int i=0; i<5; i++) { ... } i = 6; for(int i=7; i<8; i++) { ... }` :omg::wtf: what is the scope then? :doh: Thank you in advance. Regards.
I think the actual rule is that:
for (int i = 0; i < 10; i++) { }
//now i is invisible here. But in VC6 it is visible after thefor (...)
And I believe in VS .net compiler, they have complied with the standard and local variables are not visible outside anymore. this is this. -
Hi everyone! Revising some code I've found this:
for(int i=0; i That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared into the "for", how is possible to use it later? The code compile right, so Was I wrong thinking that "i" _is_declared_ into the "for"? Then, talking with the partners at work someone pointed out that in VS you can do it, but in C++Builder it wouldn't compile. The question is What is the standard? And how are declared that kinds of variables? And just testing I can do things like `for(int i=0; i<5; i++) { ... } i = 6; for(int i=7; i<8; i++) { ... }` :omg::wtf: what is the scope then? :doh: Thank you in advance. Regards.
Gizzo wrote:
If "i" is declared into the "for", how is possible to use it later?
In VS 2005 it's back to normal "i" won't be visible outside for
--Owner Drawn --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord
-
Hi everyone! Revising some code I've found this:
for(int i=0; i That caused an ASSERT(HowCanItBe) into my brain. If "i" is declared into the "for", how is possible to use it later? The code compile right, so Was I wrong thinking that "i" _is_declared_ into the "for"? Then, talking with the partners at work someone pointed out that in VS you can do it, but in C++Builder it wouldn't compile. The question is What is the standard? And how are declared that kinds of variables? And just testing I can do things like `for(int i=0; i<5; i++) { ... } i = 6; for(int i=7; i<8; i++) { ... }` :omg::wtf: what is the scope then? :doh: Thank you in advance. Regards.