Creating variable names from parameters

前端 未结 1 797
借酒劲吻你
借酒劲吻你 2021-01-28 09:41

Is it possible to create a list of variables with a parameter in the name? Something like this:

for(n=0; n< val; n++)
  int var\'n\'=0;
相关标签:
1条回答
  • 2021-01-28 10:17

    Usually, no.

    Variables names must be fixed at compile-time. They cannot depend on run-time input. So if the n in your example is input (say, from the user), what you want cannot be done.

    If you know n statically, you could do tricks with the preprocessor, or templates, or both, but you probably don't want to: How would you get to use your n variables?

    0 讨论(0)
提交回复
热议问题