References and const

Source: Internet
Author: User

Introduction to C + + references:

The reference introduces a synonym for the object. Defining a reference is similar to defining a pointer, but instead of "*" with &. The reference is an important extension of C + +.

A reference is an alias for a variable, and the action on the reference is exactly the same as the direct operation on the variable.

Declarative method of reference: type identifier & reference name = Target Traversal name

Example Description:

int A; int &p_a=a;

Note: (1):& is not an address operation, but an identification function.

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

(3): a declaration reference, which must be initialized for its process.

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

(5): Declares a reference, does not define a variable, simply means that the reference name is an alias of the variable name, it is not a data type in itself, so the reference itself does not occupy storage space, the system does not assign a storage unit to the reference. Therefore: the reference to the address, is the reference to the target variable on the address.

(6): The reference to the array cannot be established. Because an array is a collection of several elements, you cannot create a



An alias for an array.

Note:

Example: int &p_a[3]={2,3,5};

But it can be written like this: const int (&p_a) [3]={2,3,5}; But GCC compiles with-std=c++0x

Citation Essential Anatomy:

The reference is actually a constant pointer in C + +. the expression int &i=j = = int *const i=&j; and the reference is initialized because the const type variable must be initialized, and the pointer must have a value.

int main () {int i=10;             int &j=i;             j + +;             cout<<i<<j<<endl;             cout<<&i<<&j<<endl;         return 0; }

Here 's a look at the code:

int main () {int i=10;            int *const j=&i;            (*j) + +;            cout<<i<<*j<<endl;        return 0; }

Printing will skip the print address step. This requires some explanation. Because references to variables are automatically dereferenced by the compiler.

References and const

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.