<bdo id='LXtx4'></bdo><ul id='LXtx4'></ul>
  • <tfoot id='LXtx4'></tfoot>
    <legend id='LXtx4'><style id='LXtx4'><dir id='LXtx4'><q id='LXtx4'></q></dir></style></legend>

    1. <i id='LXtx4'><tr id='LXtx4'><dt id='LXtx4'><q id='LXtx4'><span id='LXtx4'><b id='LXtx4'><form id='LXtx4'><ins id='LXtx4'></ins><ul id='LXtx4'></ul><sub id='LXtx4'></sub></form><legend id='LXtx4'></legend><bdo id='LXtx4'><pre id='LXtx4'><center id='LXtx4'></center></pre></bdo></b><th id='LXtx4'></th></span></q></dt></tr></i><div id='LXtx4'><tfoot id='LXtx4'></tfoot><dl id='LXtx4'><fieldset id='LXtx4'></fieldset></dl></div>

      <small id='LXtx4'></small><noframes id='LXtx4'>

        为什么这个 C++ 工作?(同名变量)

        Why is this c++ working? (variables with the same name)(为什么这个 C++ 工作?(同名变量))

          <bdo id='vuDt9'></bdo><ul id='vuDt9'></ul>
          • <i id='vuDt9'><tr id='vuDt9'><dt id='vuDt9'><q id='vuDt9'><span id='vuDt9'><b id='vuDt9'><form id='vuDt9'><ins id='vuDt9'></ins><ul id='vuDt9'></ul><sub id='vuDt9'></sub></form><legend id='vuDt9'></legend><bdo id='vuDt9'><pre id='vuDt9'><center id='vuDt9'></center></pre></bdo></b><th id='vuDt9'></th></span></q></dt></tr></i><div id='vuDt9'><tfoot id='vuDt9'></tfoot><dl id='vuDt9'><fieldset id='vuDt9'></fieldset></dl></div>

            • <tfoot id='vuDt9'></tfoot>

                <tbody id='vuDt9'></tbody>

              1. <small id='vuDt9'></small><noframes id='vuDt9'>

                  <legend id='vuDt9'><style id='vuDt9'><dir id='vuDt9'><q id='vuDt9'></q></dir></style></legend>
                  本文介绍了为什么这个 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++ 工作?(同名变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                    <small id='s29gF'></small><noframes id='s29gF'>

                    <tfoot id='s29gF'></tfoot>
                      <bdo id='s29gF'></bdo><ul id='s29gF'></ul>
                      <i id='s29gF'><tr id='s29gF'><dt id='s29gF'><q id='s29gF'><span id='s29gF'><b id='s29gF'><form id='s29gF'><ins id='s29gF'></ins><ul id='s29gF'></ul><sub id='s29gF'></sub></form><legend id='s29gF'></legend><bdo id='s29gF'><pre id='s29gF'><center id='s29gF'></center></pre></bdo></b><th id='s29gF'></th></span></q></dt></tr></i><div id='s29gF'><tfoot id='s29gF'></tfoot><dl id='s29gF'><fieldset id='s29gF'></fieldset></dl></div>
                        <tbody id='s29gF'></tbody>

                      • <legend id='s29gF'><style id='s29gF'><dir id='s29gF'><q id='s29gF'></q></dir></style></legend>