devxlogo

The Scope of A Local Loop Counter

The Scope of A Local Loop Counter

C++ allows you to declare variables wherever you need them, thus enabling immediate initializations. A good example is a loop counter. It can (and should) be declared inside a for-statement:

 void f() {for (int i = 0; i 

In earlier stages of C++, a local variable declared this way remained accessible in its enclosing block. This was a source for bugs and name hiding. Consequently, the standard has been revised to fix this loophole, so to speak, and local variables created this way are inaccessible outside the for-statement in which they were declared. In the example above, i is inaccessible outside the loop.

devx-admin

Share the Post: