Why is this c++ working? (variables with the same name)(为什么这个 C++ 工作?(同名变量))
问题描述
好的,我想知道为什么这段代码有效,我刚刚意识到我在同一个范围内有两个同名的变量.
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++ 工作?(同名变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么这个 C++ 工作?(同名变量)
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01