Home  >  Q&A  >  body text

C++ Primer中阅读对const的引用一部分的疑惑

书中55页(第五版)写到

既然让ri引用dval,就肯定想通过ri改变dval的值,否则干什么要给ri赋值呢?如此看来,既然大家基本上不会想着把引用绑定到临时量上,C++语言也就把这种行为归为非法。

既然对某个变量的引用是为了通过该引用改变这个变量,那常量引用的意义何在?另外最后一句话“既然大家基本上不会想着把引用绑定到临时量上,C++语言也就把这种行为归为非法。”不是很明白,能不能举一个例子?

PHP中文网PHP中文网2713 days ago498

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 13:10:54

    You have to read it in conjunction with page 46. Page 46 says:

    Except for the two exceptions to be introduced in Section 2.4.1 (page 55) and Section 15.2.3 (page 534), the types of all other references must strictly match the objects to which they are bound. .

    The exception introduced on page 55 means that const references are an exception. Examples are also given in the book, on page 46:

    double dval = 3.14;
    int &refVal5 = dval;  //错误:此处引用类型的初始值必须是int型对象

    In page 55:

    double dval = 3.14;
    const int &ri = dval;

    Yes.

    Since you let ri reference dval, you definitely want to change the value of dval through ri. Otherwise, why would you assign a value to ri? From this point of view, since everyone basically does not think of binding references to temporary quantities, the C++ language also classifies this behavior as illegal

    The purpose of this paragraph is to explain why non-const references are illegal (example on page 46), not to explain the role of const constant references. The beginning of this passage is:

    The next discussion is when ri is not a constant....

    The function of constant references is mainly to prevent the referenced objects from being modified. For example, if you have an iterator, and you only want to iterate and do not want the iterator to change the element object referenced, then you need to use a constant reference.

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:10:54

    The meaning of constant reference is to obtain the value and prevent the value from changing.
    const int &i=1;
    is equivalent to
    int j = 1;
    const int &i=j;

    Make a non-const reference to a temporary variable. After the assignment changes, the referenced variable cannot be accessed, which is meaningless.

    One of the uses of const references is to pass function parameters:
    void foo(const string& s);
    You can call it like this
    s = "abc";
    foo(s);
    Or call
    foo("abc");
    instead of const reference void foo(string &). The latter call is illegal.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:10:54

    Temporary variables only exist temporarily, and their lifespan is controlled by the compiler. They are generally released after the complete expression is executed.
    So for such a temporary thing, there is no point in modifying it, because it will die after a while

    reply
    0
  • 阿神

    阿神2017-04-17 13:10:54

    Hi,
    Part of the reference is to replace the transfer of pointers to modify the value of external variables. The constant reference you mentioned is an optimization for data copying. Imagine defining an overly long string. , in order to avoid copying, use references. Use const to avoid internal modification of the function

    reply
    0
  • Cancelreply