问题描述
好的,我想知道为什么这段代码有效,我刚刚意识到我在同一个范围内有两个同名的变量.
Alright, I wanna know why this code is working, I just realized that I have two variables with the same name within the same scope.
我使用的是 g++ (gcc 4.4).
I'm using g++ (gcc 4.4).
for(int k = 0 ; k < n ; k++)
{
while(true)
{
i = Tools::randomInt(0, n);
bool exists = false;
for(int k = 0 ; k < p_new_solution_size ; k++)
if( i == p_new_solution[k] )
{
exists = true;
break;
}
if(!exists)
break;
}
p_new_solution[p_new_solution_size] = i;
p_new_solution_size++;
}
推荐答案
好的,我想知道为什么这段代码有效,我刚刚意识到我在同一个范围内有两个同名的变量.
Alright, I wanna know why this code is working, I just realized that I have two variables with the same name within the same scope.
您似乎对范围感到困惑.它们不是在同一个"范围内...... for 循环的 k 有它自己的嵌套/内部范围.更重要的是,要了解该语言为何允许这样做,请考虑:
You seem confused about scopes. They're not "within the same" scope... the for loop's k has it's own nested/inner scope. More importantly, to see why the language allows it, consider:
#define DO_SOMETHING
do { for (int i = 1; i <= 2; ++i) std::cout << i << '
'; } while (false)
void f()
{
for (int i = 1; i <= 10; ++i)
DO_SOMETHING();
}
这里,被宏DO_SOMETHING"替换的文本在与 i 相同的范围内被评估.如果您正在编写 DO_SOMETHING,您可能需要对其进行扩展以将某些内容存储在变量中,并确定标识符 i
- 显然您无法知道它是否已经存在于调用上下文中.您可以尝试选择一些更晦涩的东西,但是您会让人们使用如此复杂的变量名称,以至于他们的代码可维护性受到影响,并且无论迟早都会发生冲突.因此,该语言只是让内部作用域引入同名变量:使用最内部的匹配项,直到其作用域退出.
Here, the text substituted by the macro "DO_SOMETHING" gets evaluated in the same scope as i. If you're writing DO_SOMETHING, you may need its expansion to store something in a variable, and settle on the identifier i
- obviously you have no way of knowing if it'll already exist in the calling context. You could try to pick something more obscure, but you'd have people using such convoluted variable names that their code maintainability suffered, and regardless sooner or later there would be a clash. So, the language just lets the inner scopes introduce variables with the same name: the innermost match is used until its scope exits.
即使您不处理宏,也不得不停下来思考某个外部作用域是否已经在使用相同的名称,这也是一种痛苦.如果你知道你只是想要一个快速的操作,你可以在不考虑更大的上下文的情况下将它弹出一个独立的(嵌套的)范围(只要你那里没有真正想要使用外部范围变量的代码:如果你这样做了那么你有时可以明确指定它(如果它是由命名空间和类限定的,但如果它在一个函数体中,你最终需要更改你自己的循环变量的名称(或者在引入你的同名之前创建一个引用或其他东西)变量)).
Even when you're not dealing with macros, it's a pain to have to stop and think about whether some outer scope is already using the same name. If you know you just want a quick operation you can pop it an indepedent (nested) scope without considering that larger context (as long as you don't have code in there that actually wants to use the outer-scope variable: if you do then you can sometimes specify it explicitly (if it's scoped by namespaces and classes, but if it's in a function body you do end up needing to change your own loop variable's name (or create a reference or something to it before introducing your same-named variable)).
这篇关于为什么这个 C++ 工作?(同名变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!