Declaration method of C + + reference

Source: Internet
Author: User

A reference is an alias for a variable, and the action on the reference is the action on the target.

Declarative methods for referencing:

    Type identifier & reference name = target variable name;

  Such as:

1 int A; 2 int &ra=a;    // defines the reference RA, which is a reference to the variable A, which is the alias

Description

    • & Here is not the address operator, but the identity function, where the identity is declared a reference name;
    • The type identifier refers to the type of the target variable (that is, a of the above code);
    • When declaring a reference, it must be initialized at the same time;
    • After the reference declaration is complete, the target variable name is equal to two names, that is, the target name and the reference name;
    • Declaring a reference is not a new definition of a variable, it only means that the reference name is an alias of the target variable name, so the system does not assign a storage unit to the reference.

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

Examples: definitions and use of references.

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intA=3;7     int&b =A;8     int&c = A;//A variable can have multiple references9     int&d = b;//Reference Initialization ReferenceTencout << b <<Endl; Onecout << C <<Endl; Acout << D <<Endl; -     return 0; - } the  - //Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!! 

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

Declaration method 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.