Run the following statement and you will get an error:
Invalid initialization of non-const reference of Type 'int & 'from a temporary of Type 'int'
int &z = 12;
12 This value has no name, so it is temporary (temporary). You cannot assign a temporary variable to a reference (& Modified) type.
However, you can assign the temporary value to the reference type modified by const. Why?
Foreign websites have heated discussions on this:
Http://stackoverflow.com/questions/8293426/error-invalid-initialization-of-non-const-reference-of-type-int-from-an-rval? RQ = 1
# Include <stdio. h> template <class T> T returnself (T & V) {return V;} template <class T> int compare (const T & V1, const T & V2) {If (V1 <V2) Return-1; if (V1> V2) return 1; return 0;} int main () {int I = 1; float J = 2.0f; double K = 3.0; printf ("I is: % d \ n", returnself (I); printf ("J is: % F \ n ", returnself (j); printf ("K is: % F \ n", returnself (k); compare ("hi", "88 "); // "hi" and "hi1" are different styles. in T ret = compare (I, 10); // can't set temporary value to reference. printf ("compare I with 10, the result is: % d", RET );}~