C + + references and references as arguments to functions __jquery

Source: Internet
Author: User

Create a "reference" to a data, and his role is to alias a variable. This is C + + is an important supplement to the language.

How to create a reference

    int a = 5;
    int &b = A;
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<&a<<endl;
    cout<<&b<<endl;

The above statement states that B is a reference and prints out the values of the a,b and their addresses. After the declaration B is a alias, B and a represents the same variable, the same storage unit in memory, with the same address.

Note the & symbol as the address is the difference between the reference declaration , the 2nd line in the above program,& after the data type, is the reference declaration character. The 5th and 6 lines,& the address character. This method can be used to differentiate: only after the data type & is the reference declaration character.

Print results:

As you can see from the above results, a is the same as the B address.

Some considerations for using references:
(1) When declaring a reference, it must be initialized at the same time, and declare which variable it represents. (with one exception, a reference is not required to initialize as a function argument)
(2) After declaring a reference, you cannot make it a reference to another variable.
(3) Cannot establish a reference array.

the role of the reference:

C + + has added a reference mechanism on the basis of the language, so what is the use of the reference? It's not just a nickname for a function. Obviously not, the meaning of reference is to function as a parameter to extend the ability of the function to pass parameters. How it is achieved. This starts with the C language parameter transfer:

We know that in the C language when calling a function, there are two main forms of argument:
(1) The variable masterpiece is the actual parameter and the formal parameter
In this way the parameter is passed to the value of the variable, and the pass is one-way. If the value of the function device parameter is changed, it is not passed back to the argument. that is, if you want to implement one such function: The value of the argument changes after the function is invoked. Obviously this approach is not achievable.

As an example:
After you execute the SWAP function, you want to change the value of the variable in the main function, and write the code in this way:

#include <iostream>
using namespace std;
int main ()
{
      void swap (int, int);
      int i =3,j = 5;
      Swap (I,J);
      cout<< "i=" <<i<<endl<< "j=" <<j<<endl;
      GetChar ();
      return 0;
}

void swap (int a,int b)
{
    int temp;
    temp = A;
    a =b;
    b= temp.

Run Result:
i = 3
j = 5

Obviously the value is not brought back, and the values of I and J are not changed after execution.

(2) Transfer the address of the variable

The method parameter is a pointer variable, the argument is the address of a variable, and when the function is called, the argument is given the address of the parameter variable, and therefore the variable unit that points to the argument.
Then we modify the following procedure:

#include <iostream>
using namespace std;
int main ()
{
      void swap (int *, int*);
      int i =3,j = 5;
      Swap (&I,&J);
     cout<< "i=" <<i<<endl<< "j=" <<j<<endl;
      GetChar ();
      return 0;
}

void swap (int *a,int *b)
{
    int temp;
    temp = *a;
    *a =*b;
    *b= temp.

Run Result:
i = 5
j = 3

In the 13th line of the program, the parameter of the function defined is the pointer, in line 6th, the called function argument is the address of the variable, which realizes the exchange of I and J, but this method is not intuitive, and still is the way of "value passing", except that the address of the variable is passed.

Then, if we use the reference feature, we can simply implement this feature and it's easy to understand:
(3) reference as a function parameter

#include <iostream>
using namespace std;
int main ()
{
      void swap (int &,int&);
      int i =3,j = 5;
      Swap (I,J);
     cout<< "i=" <<i<<endl<< "j=" <<j<<endl;
      GetChar ();
      return 0;
}

void swap (int &a,int &b)
{
    int temp;
    temp = A;
    a =b;
    b= temp.

Run Result:
i = 5
j = 3

Line 13th, the formal parameter is the declaration's reference, note that the reference is not initialized, which is the special case mentioned above. In the 7th line call function in the process, the implementation of the initialization of the reference, this is the incoming argument is a variable, not a numerical value, so the true meaning of the "variable delivery."

Related Article

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.