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.
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.
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
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
You have to read it in conjunction with page 46. Page 46 says:
The exception introduced on page 55 means that const references are an exception. Examples are also given in the book, on page 46:
In page 55:
Yes.
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 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.
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.
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
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