C + + Reference detailed __c++

Source: Internet
Author: User
Tags function definition

Ext: http://www.cnblogs.com/gw811/archive/2012/10/20/2732687.html

The concept of a reference

Reference: is an alias for a variable (target), and the operation of the reference is exactly the same as the direct manipulation of the variable.

Referenced declaration method: type identifier & reference name = target variable name;

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

int A;

int &ra=a;

(1) & This is not to find the address operator, but to play the role of identity.

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

(3) When declaring a reference, it must be initialized at the same time.

(4) After the reference declaration is complete, the target variable has two names, that is, the target's original name and reference name, and the reference name cannot be used as an alias for another variable name.

(5) Declaring a reference, not a new definition of a variable, it only means that the reference name is an alias of the target variable name, it is not a data type itself, so the reference itself does not occupy the storage unit, and the system does not allocate the storage unit to the reference. Therefore: the reference to the address, that is, the target variable to find the address. &ra and &a are equal.

(6) Cannot establish a reference to an array. Because an array is a collection of several elements, you cannot create an alias for an array.

1 #include <iostream.h>
 2 void Main () {
 3     int a=5;
 4     int &b=a;
 5     b=6;
 6     cout<< "a=" <<a<< ", b=" <<b<<endl;//a=6,b=6
 7     int c=7;
 8     b=c;
 9     cout<< "a=" <<a<< ", b=" <<b<<endl;//a=7,b=7
10}
1 #include <iostream.h>
2 void Main () {
3     int a[]={1,2,3,4};
4     int &b=a;
5     //Compile Error: Cannot convert from ' int [4] ' to ' int & '
6}

application of the reference

1, reference as a parameter

An important function of a reference is as an argument to a function. The previous C language function parameter transfer is value transfer, if there are large chunks of data as a parameter transmission, the use of the scheme is often pointers, because this can avoid the whole block of data stack, can improve the efficiency of the program. But now (c + +) has added an equally efficient choice (and, in some special cases, a necessary choice), is a reference.

1 #include <iostream.h>
 2////The formal parameter P1 of the function, p2 refers to 
 3 void swap (int &p1,int &p2) {
 4     int p=p1;
 5     p1=p2;
 6     p2=p;
 7}

To call the function in the program, the call point of the corresponding calling function is called directly with the variable as the argument, without the need for the argument
The variable has any special requirements. For example, corresponding to the SWAP function defined above, the corresponding keynote function can be written as:
8 void Main () { 9 int a,b; cin>>a>>b;//Enter the value of a,b two variables (A,B);//Call the Swap function directly with a and B as arguments cout< < "a=" <<a<< "b=" <<b<<endl; 13}

When the above program runs, if the input data 10 20 and returns, then the output result is a=20,b=10.

As can be seen from the above example:

(1) The effect of passing a reference to a function is the same as passing a pointer. At this time, the parameter of the function is called the real parametric or an alias of the object in the original keynote function, so the operation of the parameter variable in the modulated function is the operation of its corresponding target object (in the keynote function).

(2) using the parameter of the reference pass function, there is no copy of the argument in memory, it is a direct operation of the argument, while using a generic variable to pass the function's arguments, when a function call is made, the parameter is required to allocate the storage unit, the parameter variable is a copy of the argument variable, and the copy constructor is called if the object is passed. Therefore, when the parameter passes the data is big, uses the reference to pass the parameter with the general variable the efficiency and occupies the space to be good.

(3) The use of pointers as a function of the parameters, although also can achieve the effect of using references, however, in the modulated function also to assign the parameters of the memory unit, and the need to reuse the "* pointer variable name \" In the form of operations, it is easy to produce errors and poor reading of the program; On the other hand, at the call point of the calling function, You must use the address of the variable as an argument. And references are easier to use and clearer.

If you want to use the reference to improve the efficiency of the program, but also protect the data passed to the function is not changed in the function, you should use the constant reference.

2, often cited

Constant reference declaration method: Const type identifier & reference name = target variable name;

A reference declared in this way cannot be modified by reference to the value of the target variable, so that the target of the reference becomes const, and the reference security is reached.

1 #include <iostream.h>
 2 void Main () {
 3     int a=1;
 4     int &b=a;
 5     b=2;
 6     cout<< "a=" <<A<<ENDL;//2
 7     int c=1;
 8     const int &d=c;
 9//    d=2;//Compile error c2166:l-value specifies const object     c=2;//correct
11}

This not only makes the code more robust, but there are other requirements.

"Example 4": Suppose you have the following function declaration:

string foo ();

void Bar (string &s);

Then the following expression will be illegal:

Bar (foo ());

Bar ("Hello World");

The reason is that Foo () and the "Hello World" string all produce a temporary object, whereas in C + +, temporary objects are const types. So the expression above is an illegal attempt to convert a const-type object to a non-const type.

Reference parameters should be defined as const if they can be defined as const.

3. Reference as return value

To return a function value as a reference, the function definition should be in the following format:

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.