References in C + +

Source: Internet
Author: User

A reference is an alias for a target 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 variable name;

As follows: Defines the reference RA, which is a reference to variable A, which is an alias.

    

intint &ra=a;
1) Declare a reference that must be initialized.

2) When the declaration is complete, the target variable has 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.

3) The reference itself is not a data type, so the reference itself does not account for the storage unit and the system does not assign a storage unit to the reference.

4) You cannot create a reference to an array. Because an array is a collection of several elements, you cannot create an alias for an array.

the nature of the reference is that the internal implementation of the reference in C + + is a constant pointer.

Type &name < ==> type * Const name (that is, the value of the pointer cannot be modified)

What are the characteristics of "reference" as a function parameter?

1) The effect of passing a reference to a function is the same as passing a pointer.

2) Use the arguments of the reference pass function, which does not produce a copy of the argument in memory, it is the operation of the argument directly. Instead of using the parameters of the generic variable transfer function, when a function call occurs, the parameter is assigned a storage unit, and the parameter variable is a copy of the argument variable; If the pass is an object, the copy constructor is also called. Therefore, it is better to use a reference than a general variable to pass the parameter efficiency and occupy space when the parameter is passed in large data.

3) Although the same effect can be achieved with pointers as function parameters. However, in the called function also to assign a storage unit to the parameter, and repeated use of the "* Pointer variable name" in the form of operation, it is easy to produce errors and poor reading of the program, on the other hand, at the call point of the keynote function, you must use the address of the variable as an argument. And references are easier and clearer.

function return value is reference (reference when left value)

1 when the function return value is a reference

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

Include <iostream>using namespacestd;intgetAA1 () {intA; A=Ten; returnA;}//Returns a copy of a that itself returns aint&getAA2 () {intA//If you return a reference on the stack, there may be a problemA =Ten; returnA;}int*getAA3 () {intA; A=Ten; return&A;}voidMain () {intA1 =0; intA2 =0; A1=getAA1 (); A2= GetAA2 ();//A2 = Tenint&AMP;A3 = GetAA2 ();////If the return stack variable cannot be the initial value of another reference, the key is to see if the variable is recycled by the compilerprintf"a1:%d \ n", A1);p rintf ("a2:%d \ n", A2);p rintf ("a3:%d \ n", A3);//equivalent to *a3 to read the value, but at this time the local variable has been recycled, so it is garbledSystem"Pause");return 0;}

2 If a static variable or global variable is returned

Can be the initial value of another reference, either as an lvalue, or as an rvalue to use.

Reference to Pointers

Reference to a pointer: a reference is the type of a pointer.

type* &name can use pointer reference, do function parameter to implement function parameter two level pointer to do output model.

#include <iostream>using namespacestd;structTeacher {Charname[ -]; intAge ;};//get the resource in the called functionintGetteacher (Teacher * *p) {Teacher*tmp =NULL; if(p = =NULL) {        return-1; } tmp= (Teacher *)malloc(sizeof(Teacher)); if(TMP = =NULL) {        return-2; } tmp->age = -; //P is the address of the actual parameter of the argument to the value of the indirect modification argument*p =tmp;}//pointer references do function argumentsintGetTeacher2 (teacher* &MYP) {    //Assigning a value to MYP is equivalent to assigning a value to pT1 in the main functionMYP = (Teacher *)malloc(sizeof(Teacher)); if(MYP = =NULL) {        return-1; } MYP->age = $;}voidFreeteacher (Teacher *pT1) {    if(PT1 = =NULL) {        return ; }     Free(pT1);}voidMain () {Teacher*PT1 =NULL; //1 level Two pointers in the C languageGetteacher (&pT1); cout<<"Age :"<<pT1->age<<Endl;    Freeteacher (pT1); //2 References in C + + (reference to pointers)//the nature of the reference indirectly assigns the following 2 conditions to let the C + + compiler do it for our programmers. GetTeacher2 (pT1); cout<<"Age :"<<pT1->age<<Endl;    Freeteacher (pT1); cout<<"Hello ..."<<Endl; System ("Pause");}
Frequently cited

1 variable Initial const reference can be used in C + +

Const Type & name = var;

Const references Let variables have read-only properties

int Ten ; Const int &b = A;  // Const reference  using variable a initialization //int *p = (int *) &b;  One // Err //  // can only be changed with a pointer

2 using literal literals to initialize a const reference

 // Reference is a memory space alias literal 10 no memory space no way to do a reference to constintten; 
Conclusion:

1) const & name equals const int * Const name;

2) ordinary reference equivalent to int* const name;

3) When a const reference is initialized with a constant (literal), the C + + compiler allocates space for the constant, and the literal is placed in the code area (no memory space), and the reference name is used as the alias for the space.

4) After initializing a const reference with a literal, a read-only variable is generated.

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.