Changing variable name inside a loop

后端 未结 3 387
误落风尘
误落风尘 2021-01-29 14:20

I\'m trying to create a loop that will will create a new variable but also change the name of the variable, such as increasing in value, automatically. Not sure if this is possi

3条回答
  •  甜味超标
    2021-01-29 15:03

    You can't. It's not possible in C++.

    To declare and initialize a variable in each iteration is however possible:

    for (int i = 0; i != 10; ++i) {
            int var = i; // Declare 'var' and assign value of 'i' to it.  
    }                    // 'var' object goes out of scope and is destroyed
    

    Names are visible from the point where they are declared until the end of the scope in which the declaration appears. Names have scope and Objects have lifetimes.

提交回复
热议问题