为什么这个c ++工作? (具有相同名称的变量) [英] Why is this c++ working? (variables with the same name)

查看:159
本文介绍了为什么这个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.

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 << '\n'; } 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.

即使你不处理宏,这是一个痛苦,必须停止,并考虑一些外部范围是否已经使用相同的名称。如果你知道你只想要一个快速操作,你可以弹出它独立的(嵌套)作用域,而不考虑更大的上下文(只要你没有代码在那里实际想使用外部scope变量:如果你做那么你有时可以明确地指定它(如果它的名称空间和类的范围,但如果它在一个函数体,你最终需要改变自己的循环变量的名称(或创建一个引用或一些东西,然后才介绍同名变量))。

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 ++工作? (具有相同名称的变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆