Reference initialization, right-left value, Function Array Parameters

Source: Internet
Author: User

Difference between l-value and r-value
The left value refers to the expression that references an object, which can be placed on the left of the value assignment, for example, * (p + 1) = 7, a variable without a name (* (p + 1) the expression must be a type object) is assigned, but the left value is not necessarily assigned, because the left value can reference a constant. All references are left values.
The right value is the expression value (not a reference), which can be placed on the right side of the value assignment.
All left values can be right values, and vice versa.
Int I, j, * P;
I = 7; // correct. A variable name, I, is an L-value.
7 = I; // error. A constant, 7, is an R-value.
J * 4 = 7; // error. The expression J * 4 yields an R-value.
* P = I; // A dereferenced pointer is an L-value.
Const int CI = 7; // declare a const variable.
Ci = 9; // error. CI is a nonmodifiable L-Value

Initialization of common references and const references
When the initial expression of reference is a left value (an object, you can obtain its address), Initialization is very simple. The initial formula of common T & must be of the T type. Cosnt T & does not have to be a left value or even a T type. In this case, follow these steps.
(1) first, apply implicit type conversion to type T if needed.
(2) Save the result to a temporary variable of type T.
(3) Use this temporary variable as the initialization value.
For example
Double & d = 1; // error. d is the left value and cannot be initialized with the right value 1.
Const double & cd = 1; // OK

The next Initialization is explained as follows:
Double temp = double (1); // first create a temporary variable with the correct data type
Const double & cd = temp; // use this temporary variable as the initial formula of cd. temp is the right value.

A major feature of the Left value is that it can be assigned a value, while const just castrated this feature, making the expression a right value, so you can use the right value for initialization.

 

=== 7.3.1 reference parameter ===
Function reference parameters
Declaring a parameter as a reference actually changes the default parameter passing mechanism by value. When passing by value, the function operates on the local copy of the real parameter. When a parameter is referenced, the function receives the left value of the real parameter rather than the copy of the value.
This means that the function knows the location of the real parameter in the memory, so it can change its value or obtain its address.
Int obj;
Void frd (double &);
Int main ()
{
Frd (obj); // error: the parameter must be const double &
Return 0;
}
The call to frd () is incorrect. If the real parameter type is int, it must be converted to double to match the type of the referenced parameter,
The conversion result is a temporary value. Because this reference is not of the const type, the temporary value (not the left value) cannot be used to initialize the reference.

When can I specify a parameter as a reference for comparison:
1. Allow changing the value of a real Parameter
2. Return additional results to the main function
3. Passing large class objects to Functions

Pointer or reference is an important distinction between parameters:
The reference must be initialized to point to an object. Once initialized, it can no longer point to other objects, and the pointer can point to a series of different objects.
Therefore, if a parameter may point to different objects in a function, or this parameter may not point to any object, you must use the pointer parameter.

 

===7.3.3Array parameters===
In C ++, an array will never be passed by value. It is a pointer that passes the first element (accurately 0th ).
The following statement
Void putValues (int [10]); // when the compiler checks the parameter type of the real parameter type, the length of the array is not checked. The editor ignores 10
Considered by the compiler
Void putValues (int *);
The length of the array has nothing to do with parameter declarations, so the following three declarations are equivalent.
// Three equivalent putValues () Statements
Void putValues (int *);
Void putValues (int []);
Void putValues (int [10]);

Three Mechanisms for passing array Length
1. provide an additional parameter containing the array length: void putValues (int [], int size );
2. Declare the parameter as an array reference: void putValues (int (& arr) [10]);
When a parameter is an array reference, the array length becomes a part of the parameter and real parameter type. The Compiler checks whether the length of the array real parameter matches the length specified in the function parameter type.
// An array with 10 int values
// Parameter is a reference to an array of 10 ints
Void putValues (int (& arr) [10]); // an array that only accepts 10 int values.
Int main (){
Int I, j [2];
PutValues (I); // error: the real parameter is not an array of 10 int values.
PutValues (j); // error: the real parameter is not an array of 10 int values.
Return 0;
}

3. Use abstract containers
Void putValues (const vector <int> & vec );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.