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

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

      <tfoot id='Sx7sv'></tfoot>

      <legend id='Sx7sv'><style id='Sx7sv'><dir id='Sx7sv'><q id='Sx7sv'></q></dir></style></legend>

        为什么重新设置参考是非法/不道德的?

        时间:2023-12-02

              <tbody id='ufnT1'></tbody>

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

                  <tfoot id='ufnT1'></tfoot>

                • <small id='ufnT1'></small><noframes id='ufnT1'>

                • 本文介绍了为什么重新设置参考是非法/不道德的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  为什么引用不能在 C++ 中重新安装

                  我正在尝试或多或少地交换两个参考变量(作为实践,我本可以交换实际变量).我尝试通过创建一个临时变量并使一个引用与另一个引用相等来做到这一点,但这被编译器拒绝了.下面是一个例子:

                  I am trying to more or less swap two reference variables (as practice, I could have swapped the actual variables). I tried doing this by making a temporary variable and making one of the references equal the other, but this got shot down by the compiler. Here is an example:

                  void Foo()
                  {
                     //code
                     int& ref1 = a;
                     int& ref2 = b;
                     int temp;
                  
                     temp = ref1;
                     ref1 = ref2;
                     ref2 = temp;
                  
                     //or, better yet
                     std::swap(ref1, ref2);
                  }
                  

                  我遇到了一个错误,并查看了 faq lite.它详细说明它们不能重新安装,但没有解释原因.为什么?

                  I got an error, and looked on the faq lite. It details that they cannot be reseated, but does not explain why. Why?

                  这里是 Faq Lite 的链接以供参考 (<;---,明白了吗?)

                  Here is a link to the Faq Lite for reference (<---, get it?).

                  推荐答案

                  因为没有语法可以做到:

                  Because there is no syntax to do it:

                  int x = 0;
                  int y = 1;
                  int & r = x;
                  

                  现在如果我说:

                  r = y;
                  

                  我将 y 的值赋给 x.如果我想重新安装,我需要一些特殊的语法:

                  I assign the value of y to x. If I wanted to reseat I would need some special syntax:

                  r @= y;    // maybe?
                  

                  由于使用引用的主要原因是作为函数的参数和返回类型,在这不是问题的情况下,C++ 的设计者似乎并不认为这是一条值得走的路.

                  As the main reason for using references is as parameters and return types of functions, where this is not an issue, it didn't seem to C++'s designers that this was a path worth going down.

                  这篇关于为什么重新设置参考是非法/不道德的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 C++ 函数返回对象的正确方法是什么? 下一篇:如何声明相互引用的类?

                  相关文章

                  1. <legend id='ZqEEU'><style id='ZqEEU'><dir id='ZqEEU'><q id='ZqEEU'></q></dir></style></legend>

                    <tfoot id='ZqEEU'></tfoot>
                      <bdo id='ZqEEU'></bdo><ul id='ZqEEU'></ul>

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

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