The role and usage of C + + references

Source: Internet
Author: User
Tags define function

A reference is an alias for a variable (the target), 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;

For example:

12 intq;int&ra=a;

Description

    • & Here is not an address operation, but an identification function.
    • The type descriptor refers to the type of the target variable.
    • When declaring a reference, it must be initialized at the same time.
    • When the reference declaration is complete, the target variable name is equal to two aliases, that is, the target name and the application name, and the reference name cannot be used as an alias for the other variable names.
    • Declaring a reference is not a new definition of a variable, it simply means that the reference name is an alias of the target variable name, it is not a data type in itself, so the reference itself does not account for the storage unit, and the system does not assign a storage unit to the reference. Therefore: to

      A reference to an address is an address to a target variable. &ra is equal to &a.

    • 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.

Referencing apps

    1. Reference as parameter

An important function of a reference is as a function parameter. The previous C language function parameter passing is the value of the pass, if there are large chunks of data passed as parameters, the use of the scheme is often a pointer, because this can avoid the whole piece of data stack,

Can improve the efficiency of the program. But now (c + +) has added an equally efficient choice (which in some special cases is a necessary choice), which is a reference.

Cases

void swap (int &p1,int &p2)//The formal parameter of the function P1, p2 are reference  {int p;p=p1;p1=p2;p2=p;}

To call this function in a program, the call point of the corresponding keynote function is called directly with the variable as an argument, without any special requirement of the real parametric.

For example: corresponding to the SWAP function defined above, the corresponding keynote function can be written as:

Main () {  int A, b;  cin>>a>>b;  Swap (A, b);//Call the Swap function directly with A/b as an argument  cout<<a<< ' <<b;        }
    1. Passing a reference to a function and passing a pointer to a function is the same effect. At this point, the parameter of the function is used as the real parametric or an alias of the object in the original Melody function, so the operation of the parameter variable is the operation of the corresponding target object in the modulated function.
    2. Using the arguments of the reference transfer function, there is no copy of the actual argument in memory, it is directly to the argument operation, while the parameters of the generic variable transfer function are used, when a function call occurs, the parameter is allocated a storage unit and the parameter variable is an argument

      A copy of the variable, and if the object is passed, the copy constructor is also called. Therefore, when the parameter passes the data is large, uses the reference to pass the parameter with the general variable efficiency and occupies the space to be good.

    3. Although the use of pointers as parameters of functions can also be achieved with the use of reference effects, but in the function of the parameter is also assigned to the parameter storage unit, and need to reuse the "* pointer variable name" in the form of operations, which is prone to error and program

      Reading is poor; 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 to use and clearer.

2. Reference as return value

To return a function value as a reference, the function definition is formatted as follows:

Type identifier & function name (formal parameter list and type description)

{function Body}

Description

1 returns the function value as a reference, which needs to be added & in front of the function name when defining the function

2 The greatest benefit of returning a function value with a reference is that it does not produce a copy of the returned value in memory.

Cases

#include <iostream.h>float temp;float fn1 (float R);//Declare Function fn1float &fn2 (float r); Declare function fn2float fn1 (float R)// Defines the function fn1, which returns the function value {    temp= (float) (r*r*3.14) in the form of a return value;    return temp;} Float &fn2 (float R)//define function FN2, which returns the function value {    temp= (float) (r*r*3.14) as a reference;    return temp;} void main ()//main function {    float A=FN1 (10.0)//system generates a copy of the value to return (that is, temporary variable)    float &b=fn1 (10.0)//May error,// Cannot return a temporary variable or a reference to a local variable from the called function    float c=fn2 (10.0);//3rd case, the system does not generate a copy of the return value    //You can return a reference to a global variable from the called function    float &d= FN2 (10.0); In the 4th case, the system does not generate a copy of the return value    //can return a reference to a global variable from the    cout<<a<<c<<d function;}

The role and usage of C + + 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.