<tfoot id='iQEEg'></tfoot>

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

    1. <legend id='iQEEg'><style id='iQEEg'><dir id='iQEEg'><q id='iQEEg'></q></dir></style></legend>
      1. <i id='iQEEg'><tr id='iQEEg'><dt id='iQEEg'><q id='iQEEg'><span id='iQEEg'><b id='iQEEg'><form id='iQEEg'><ins id='iQEEg'></ins><ul id='iQEEg'></ul><sub id='iQEEg'></sub></form><legend id='iQEEg'></legend><bdo id='iQEEg'><pre id='iQEEg'><center id='iQEEg'></center></pre></bdo></b><th id='iQEEg'></th></span></q></dt></tr></i><div id='iQEEg'><tfoot id='iQEEg'></tfoot><dl id='iQEEg'><fieldset id='iQEEg'></fieldset></dl></div>
          <bdo id='iQEEg'></bdo><ul id='iQEEg'></ul>
      2. 返回引用与返回值 C++ 之间的区别

        Difference between returning reference vs returning value C++(返回引用与返回值 C++ 之间的区别)
      3. <tfoot id='9sp1v'></tfoot>

          <legend id='9sp1v'><style id='9sp1v'><dir id='9sp1v'><q id='9sp1v'></q></dir></style></legend>
            <bdo id='9sp1v'></bdo><ul id='9sp1v'></ul>

                <small id='9sp1v'></small><noframes id='9sp1v'>

                  <tbody id='9sp1v'></tbody>
                • <i id='9sp1v'><tr id='9sp1v'><dt id='9sp1v'><q id='9sp1v'><span id='9sp1v'><b id='9sp1v'><form id='9sp1v'><ins id='9sp1v'></ins><ul id='9sp1v'></ul><sub id='9sp1v'></sub></form><legend id='9sp1v'></legend><bdo id='9sp1v'><pre id='9sp1v'><center id='9sp1v'></center></pre></bdo></b><th id='9sp1v'></th></span></q></dt></tr></i><div id='9sp1v'><tfoot id='9sp1v'></tfoot><dl id='9sp1v'><fieldset id='9sp1v'></fieldset></dl></div>
                  本文介绍了返回引用与返回值 C++ 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  关于为什么有必要从函数返回引用的问题.

                  Question about why is it necessary at all to return a reference from a function.

                  以下代码的行为完全相同,如果我们将第 9 行和第 16 行中的 int& 替换为 int.

                  Following code behaves exactly the same, if we replace int& with int in line9 and line16.

                  在我的示例代码中是否正确,返回引用与值无关紧要?在什么样的例子中它会开始重要?

                  Is it true in my example code, returning reference vs value doesnt matter? In what kind of example it will start to matter?

                  在我看来,我们不能返回函数的局部变量的引用,因为局部变量将超出调用者的范围.因此,只返回调用者可以看到的变量的引用(在范围内)才有意义,但是如果函数的调用者可以看到该变量,那么它就不需要返回(?)(或者这是返回是为了保持代码整洁?)

                  In my mind, we cant return the reference of a local variable of the function, since the local variable will be out of scope for the caller. Therefore, it only make sense to return a reference of a variable that the caller can see (in scope), but if the caller of the function can see the variable, then it doesnt need to be returned(?) (or is this returning done for the sake of keeping the code neat and tidy?)

                  相关链接:返回引用是个好主意吗?

                  #include <iostream>
                  using namespace std;
                  
                  class myClass{
                  private:
                      int val;
                  public:
                      myClass(int);
                      int& GetVal();
                  };
                  
                  myClass::myClass(int x){
                      val = x;
                  }
                  
                  int& myClass::GetVal(){
                      return val;
                  }
                  
                  int main()
                  {
                      myClass myObj(666);
                      cout << myObj.GetVal() << endl;
                      system("pause");
                      return 0;
                  }
                  

                  推荐答案

                  不同的是,当你返回一个引用时,你可以赋值给GetVal()的结果:

                  The difference is that, when you return a reference, you can assign to the result of GetVal():

                  myObj.GetVal() = 42;
                  

                  你也可以保留返回的引用,以后用它来修改myObj.val.

                  You can also keep the returned reference around, and use it to modify myObj.val later.

                  如果 GetVal() 要按值返回 val,这一切都不可能.

                  If GetVal() were to return val by value, none of this would be possible.

                  这是否是可取的,或者是否确实是好的设计,完全是一个不同的问题.

                  Whether any of this is desirable, or indeed good design, is a different question altogether.

                  请注意,您的示例与 链接中的代码非常不同问题 -- 该代码返回无效引用,这无疑是个坏主意.

                  Note that your example is very different to the code in the linked question -- that code returns an invalid reference and is unequivocally a bad idea.

                  这篇关于返回引用与返回值 C++ 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

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

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

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

                              <tbody id='XA1f9'></tbody>