C + + references (References)

Source: Internet
Author: User

1.c++ References:

A reference is an alias to a variable (the target), which is equivalent to the same person having two names, regardless of which name actually refers to the same person. Similarly, on a reference, the action on the reference is exactly the same as the direct action on the variable, so modifying the value of the reference is modifying the value of the variable

From the memory point of view, the reference and the variable name point to the same memory, the operation of the reference address and the address of the variable name is the same memory operation. The reference itself occupies the storage unit, but you have no way to access the space occupied by the reference from the C + + level.

2. Reference to the declaration:

Type identifier & Reference name = initial value;
& is a reference specifier that indicates that the data type is a reference type

PS:   The reference must be initialized when declaring a reference, since the reference cannot modify the int &rn once it is declared ; Wrong, uninitialized

the type in the declaration statement must be the same as the variable type of the initial value     int n = ten;d ouble &rn = n; Wrong, the declared reference type is double, and the type of the initial value is int

intMain () {intx=Ten; int&a=x;//Create a reference to Xcout<<&a<<" "<<a<<" "<<* (&a) <<endl;//0xbfc662b8 Ten    intn=Ten; int*p=&n;//Pointer to n: The value of the pointer is the address of Ncout<<p<<" "<<*p<<endl;//0XBFC662BC Ten    int*&fp=p;//To establish a reference to a pointer: &FP refers to the address of the pointer variable p,cout<<&fp<<" "<<fp<<" "<<*&fp<<endl;//0xbfc662c0 0XBFC662BC 0XBFC662BC    int&r=*New int; //new opened the nameless Memory building reference R= -;//equivalent to int r=100;cout<<&r<<" "<<r<<Endl; //destroy R open memory can be used with delete &r;}

3. Limitations of reference use:

(1) Cannot create a reference to an array

(2) reference cannot be established

4. Comparison of pointers and references:

Different points:
1>. Pointers can be re-assigned, and references cannot be modified once they are declared initialized;
2>. The pointer can be empty (null), and the reference cannot;
3>. Pointers can be declared without initialization, and references cannot;
Same point:
Can operate directly on the entity.

C + + references (References)

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.