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

      <legend id='f3S4j'><style id='f3S4j'><dir id='f3S4j'><q id='f3S4j'></q></dir></style></legend>
        <bdo id='f3S4j'></bdo><ul id='f3S4j'></ul>
    1. <i id='f3S4j'><tr id='f3S4j'><dt id='f3S4j'><q id='f3S4j'><span id='f3S4j'><b id='f3S4j'><form id='f3S4j'><ins id='f3S4j'></ins><ul id='f3S4j'></ul><sub id='f3S4j'></sub></form><legend id='f3S4j'></legend><bdo id='f3S4j'><pre id='f3S4j'><center id='f3S4j'></center></pre></bdo></b><th id='f3S4j'></th></span></q></dt></tr></i><div id='f3S4j'><tfoot id='f3S4j'></tfoot><dl id='f3S4j'><fieldset id='f3S4j'></fieldset></dl></div>
        <tfoot id='f3S4j'></tfoot>
      1. 为什么我可以为引用分配一个新值,以及如何使引用引用其他内容?

        Why can I assign a new value to a reference, and how can I make a reference refer to something else?(为什么我可以为引用分配一个新值,以及如何使引用引用其他内容?)

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

              • <bdo id='NAui9'></bdo><ul id='NAui9'></ul>
              • <small id='NAui9'></small><noframes id='NAui9'>

                  <tfoot id='NAui9'></tfoot>

                  本文介绍了为什么我可以为引用分配一个新值,以及如何使引用引用其他内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有几个与在 C++ 中使用引用相关的问题.

                  I have couple of questions related to usage of references in C++.

                  1. 在下面显示的代码中,它是如何工作的并且不会在 q = "world"; 行出现错误?

                  #include <iostream>
                  
                  using namespace std;
                  
                  int main()
                  {
                    char *p = "Hello";
                    char* &q = p;
                    cout <<p <<' '<<q <<"
                  ";
                    q = "World"; //Why is there no error on this line
                    cout <<p <<' '<<q <<"
                  ";
                  }
                  

                  1. 如何将引用 q 重新初始化为其他内容?

                  1. How can a reference q be reinitialized to something else?

                  字符串文字 p = "Hello" 不是常量还是只读空间?所以如果我们这样做,

                  Isn't the string literal, p = "Hello", a constant or in read-only space? So if we do,

                  q = "World";
                  

                  p 处应该是常量的字符串不会改变吗?

                  wouldn't the string at p which is supposed to be constant be changed?

                • 我读过 C++ 引用类型变量,因为它们无法重新初始化或重新分配,因为它们在内部"存储为常量指针.所以编译器会报错.

                • I have read about C++ reference type variables as they cannot be reinitialized or reassigned, since they are stored 'internally' as constant pointers. So a compiler would give a error.

                  但实际上如何重新分配引用变量?

                  But how actually a reference variable can be reassigned?

                  int i;
                  
                  int &j = i;
                  
                  int k;
                  
                  j = k; //This should be fine, but how we reassign to something else to make compiler flag an error?
                  

                  我正在尝试获取此参考资料,因此可能遗漏了一些相关的关键内容,所以这些问题.

                  I am trying to get hold of this reference, and in that maybe missed some key things related, so these questions.

                  因此,任何清除此问题的指针都会很有用.

                  So any pointers to clear this up, would be useful.

                  推荐答案

                    • a) 它不能,您引用的行不会改变引用 q,它会改变 p.
                    • b) 不,文字是常量,但 p 是指向文字的指针.指针可以改变,指向的东西不能.q = "world"; 使指针 p 指向别的东西.
                    • a) It cannot, the line you quote doesn't change the reference q, it changes p.
                    • b) No the literal is constant, but p is a pointer which points at a literal. The pointer can be changed, what is being pointed to cannot. q = "world"; makes the pointer p point to something else.

                  你似乎认为这段代码

                  int i;
                  int &j = i;
                  int k;
                  j = k;
                  

                  正在重新分配引用,但事实并非如此.它将k 的值赋值给ij 仍然指的是i.我猜这是你的主要误解.

                  is reassigning a reference, but it isn't. It's assigning the value of k to i, j still refers to i. I would guess that this is your major misunderstanding.

                  这篇关于为什么我可以为引用分配一个新值,以及如何使引用引用其他内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  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++ 引用发生变化)
                    <tbody id='yZw38'></tbody>
                    <bdo id='yZw38'></bdo><ul id='yZw38'></ul>

                      <tfoot id='yZw38'></tfoot>

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

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

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