An explanation of C + + reference &

Source: Internet
Author: User

References in C + +:

The reference introduces a synonym for the object. Defining a reference is similar to defining a pointer, but using & instead of *. The reference (reference) is an important extension of C + +. A reference is an alias for a variable (the target), and the action on the reference is exactly the same as the direct operation on the variable.

The format is: type & Reference variable name = variable name that has been defined .

Features of the reference:

① A variable is preferable to multiple aliases.

The ② reference must be initialized.

③ references can only be referenced once at initialization time and cannot be changed to refer to other variables instead.

① Base Reference:

void TestReference1 () {     int a = 1;     int& B = A;     cout<< "a:address->" <<&a<< Endl;     cout<< "b:address->" <<&b<< Endl;     A = 2;     b = 3;     int& C = b;//refers to a reference variable, alias     c = 4;}

  

②const References:

void TestReference2 () {     int d1 = 4;     const int & d2 = D1;     D1 = 5;//d1 change, the value of D2 will change.     //D2 = 6;//cannot assign a value to a constant (the amount that cannot be modified).     const int d3 = 1;     const int & d4 = D3;     INT&D5 = D3;     const int & d6 = 5;//constants have constancy, only constant references can reference constants     double d7 = 1.1;     int& D8 = D7;//d7 is a double type, D8 is a temporary variable to be generated when INT,D7 is assigned to D8                   ///That is, the D8 refers to this temporary variable with constancy, so it cannot be assigned a value.     const int& D9 = D7;}

  

③ References as arguments:

1. "Value Pass" if the parameter is a non-referenced value, then the value of the local temporary variable to receive the argument is void swap (int left, int right)//value is passed in such a way that the interchange cannot be implemented because the parameters left and right are copied a temporary copy when the parameter is The interchange is a copy value because it is a temporary variable function exit, the variable pin {                                //destroys, and does not affect the external left and right values.     int temp = left;     left = right;     right = temp;} 2. "Reference pass" if the parameter is a reference type, the parameter is the alias of the argument. void Swap (int& left, int& right)//use of a reference, do not make a temporary copy,& instructions here is just another name of the original parameter, so modify the value of the variable directly on the basis of the original parameter, reducing the cost of replication, Accelerate program execution efficiency. {     int temp = left;     right = left;     left = temp;} 3. "Pointer pass" void Swap (int* pleft, int* pright)//incoming address, because the address is unique, so the pointer through the address of access to modify its contents. {     int temp = *pleft;     *pleft = *pright;     *pright = temp;}

  

Advantages of the reference:

1, function parameters. The reference pass ratio is highly efficient to pass.

2, function return value.

    (1)   the life cycle of the returned variable is greater than the life cycle of the function;

      (2) cannot return local variables;

      (3) do not easily return to the heap space to prevent memory leaks.

Attention:

(1) If a stack variable is returned, it cannot be the initial value of another reference and cannot be used as an lvalue.

(2) If a static variable or a global variable is returned, it can be the initial value of another reference and can be used as an lvalue or rvalue.

The use of the reference is convenient and must be used with caution:

(1) & Here is not the address calculation, but the role of the logo.

(2) The type identifier refers to the type of the target variable.

(3) When declaring a reference, it must be initialized at the same time and cannot be set to NULL.

(4) When the declaration is complete, the target variable name is equivalent to two names, that is, the target name and reference name, and the reference name cannot be used as an alias for the other variable names.

(5) The address of the reference, that is, the target variable to find the address. That is, the reference name is an alias of the target variable name. The reference is defined as saying that the reference does not occupy any memory space, but the compiler will generally

This is actually a const pointer, which is a pointer to a position that is immutable, so the reference actually uses the same memory as the generic pointer.

(6) An array of references cannot be established. Because an array is a collection of several elements, you cannot create a collection that consists of references, but you can establish a reference to the array.

(7) Reference common use: As a function of the parameters, functions of the return value.

Summarize:

1. Do not return a reference to a temporary variable.

2. If the scope of the returned object is the same as the current function, it is best to return it using a reference because it is more efficient.

* Reference and pointer differences and contact (written test hotspot)

1. A reference can only be initialized once at the time of definition and cannot be changed to point to other variables (mindedness); Variable value of pointer variable.

2. The reference must point to a valid variable (cannot be null) and the pointer can be empty.

3. The sizeof pointer object and the Reference object do not have the same meaning. sizeof refers to the size of the variable that is pointed to, and the sizeof pointer is the size of the object address.

4. Pointers and reference self-increment (+ +) self-subtraction (-) have different meanings.

5. References are more secure than pointers.

Pointers are more flexible than references, but they also have a high risk. Be sure to check that the pointer is empty (NULL) when using the pointer, and that the pointer is best placed after the space is recycled

0, in order to avoid the occurrence of the wild pointer memory leaks and other issues.

References and pointers differ and relate to:

★ Different points:

1. The pointer is an entity, and the reference is only an individual name;

2. The reference does not need to dereference (*), the pointer needs to be dereferenced;

3. References can only be initialized once at the time of definition, and then immutable; pointers are variable;

4. The reference does not have a const, the pointer is Const;const modified pointer is immutable;

5. The reference cannot be null, the pointer can be empty;

6. "sizeof Reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object to which it is pointing);

7. The pointer and the reference self-increment (+ +) operation has different meanings;

8. From memory allocation: The program allocates memory regions for pointer variables, whereas references do not need to allocate memory areas.

★ Same point:

Both are the concept of the address, the pointer points to the memory, the content is the address of the referred memory, the reference is a block of memory alias.

An explanation of C + + reference &

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.