References in C + +

Source: Internet
Author: User

One, the basic knowledge of C + + reference

1. Basic concepts of reference

1. The so-called reference is actually the "alias" of the variable. References and variables correspond to the same memory, modify the referenced values, and the values of the variables change, similar to pointers.

2. References must be initialized at the time of definition, and are bound together with the referenced objects after initialization.

3. The reference itself is not an object, and the referenced reference cannot be defined.

2. Meaning of references

1. References exist as aliases to other variables, so they can be used instead of pointers on some occasions.

2. References are more readable and useful than pointers.

3. How references are defined

Data type & Reference name = variable;

# include<iostream>usingnamespace  std; int Main () {    //  define variable a    intten;     // Defining reference B    int &b = A;     return 0 ;}

Second, the nature of the quote

1. Analysis of the nature of references

# include<iostream>using namespacestd;intMain () {//define variable a    intA =Ten; //Defining reference B    int&b =A; //once the reference is initialized, it does not change the object it is bound to, so the reference would like a const-modified constantcout <<"&a ="<< &a <<Endl; cout<<"&b ="<< &b <<Endl; //by printing the address, the variable and the referenced address are found to be the same, so the reference is essentially a pointer    return 0;}

2. Analysis of the nature of references

1. The reference is not allowed to be modified after initialization, so the reference is a constant.

2. The address of the reference is the same as the address of the variable itself. So the reference is a pointer.

3. The nature of the reference is a constant pointer, except that the C + + compiler helps us with the automatic fetch address operation and the dereference operation.

4. The nature of the above case: int &b = A; equivalent to int * Const B = &a;

Three, the focus of the quote

1. Reference as function parameter

A reference function parameter can be used instead of a pointer to manipulate the reference inside a function to modify the value of the external variable of the function.

2. Reference as function return value (here involves the copy constructor of the class)

The reference is a function return value, and if a stack variable is returned, a copy of the variable is actually returned.

3. Pointer reference

A reference is a pointer type, for example: Teacher Teacher = {"Wang Gang", 21}; Teacher * &t = &teacher;

4. Frequently cited

Use a variable to initialize the reference so that the reference has a read-only property, protecting the object pointed to by the reference and preventing it from being modified inside the function.

Use literal initialization of the reference, for example: const int &a = 10; Yes, this will allocate memory space for literal constants during compilation, as mentioned in the const in the previous section.

References in C + +

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.