Home>

With the variable name,Why do we need an alias?The reason c++ adds reference types, It is mainly used as a function parameter,The ability to pass data to extended functions.

So far we have introduced two cases of function parameter passing.

1) Use the variable name as an argument and a formal parameter

At this time, the parameter is passed to the value of the variable,Delivery is one-way.If the value of a formal parameter changes during the execution of the function,It is not passed back to the argument.Because when calling a function,The formal and actual parameters are not the same storage unit.

[Example] It is required to exchange the values ​​of the variables i and j.The following program cannot fulfill this requirement.

#include<iostream>
using namespace std;
int main ()
{
  void swap (int, int);//function declaration
  int i=3, j=5;
  swap (i, j);//Call the function swap
  cout<i<""<j<endl;//The values ​​of i and j are not interchanged
  return 0;
}
void swap (int a, int b) //Attempt to swap the values ​​of the formal parameters a and b,Realize the value exchange of the actual parameters i and j
{
  int temp;
  temp=a;//The following 3 lines are used to achieve the value exchange of a and b
  a=b;
  b=temp;
}

The values ​​of output 3 5i and j are not interchanged at runtime.

To solve this problem,Use the method of passing the address of the variable.

2) Passing a pointer to a variable

Formal parameters are pointer variables,The argument is the address of a variable,When calling a function,The formal parameter (pointer variable) points to the actual variable unit.The procedure is shown in Example 6.19.

[Example] Using pointer variables as formal parameters,Achieve the exchange of the values ​​of two variables.

#include<iostream>
using namespace std;
int main ()
{
  void swap (int *, int *);
  int i=3, j=5;
  swap (&i,&j);//The argument is the address of the variable
  cout<i<""<j<endl;//The values ​​of i and j have been swapped
  return 0;
}
void swap (int * p1, int * p2) //The formal parameter is a pointer variable
{
  int temp;
  temp=* p1;//The following 3 lines are used to exchange the values ​​of i and j
  * p1=* p2;
  * p2=temp;
}

The combination of formal participation parameters is shown in the figure.

This method of combining virtual and real is still "value passing", except that the value of the actual parameter is the address of the variable.The variables (i and j) in the main function are accessed through the parameter pointer variables, and their values ​​are changed.This will get the correct result,But conceptually it is a circle,Not so straightforward.

There are "value parameter" and "variable parameter" (that is, var parameter) in pascal language, corresponding to two different transmission methodsThe former uses value passing,The latter uses address passing.In the C language, there are only "value parameters" and no "variable parameters", and all use value transfer.c++ takes a reference variable as a function parameter,This makes up for this deficiency.

C++ provides the (3) method of passing data to functions, that is, passing aliases of variables.

[Example] Use "reference parameter" to realize the value exchange of two variables.

#include<iostream>
using namespace std;
int main ()
{
  void swap (int&, int&);
  int i=3, j=5;
  swap (i, j);
  cout<""i<<<<<<<<<<endl;
  return 0;
}
void swap (int&a, int&b) //formal parameter is a reference type
{
  int temp;
  temp=a;
  a=b;
  b=temp;
}

The output is:

 i=5 j=3

In the parameter list of the swap function, declare that a and b are references to integer variables.

In fact, in the combination of virtual and real, the address of the actual parameter i is passed to the formal parameter a, so that the address of the formal parameter a takes the address of the actual parameter i, so that a and i share the same unit.Similarly, the address of the actual parameter j is passed to the formal parameter b, so that the address of the formal parameter b takes the address of the actual parameter j, so that b and j share the same unit.This is how the address is passed.For easy understanding,In layman's terms:pass the name of the variable i to the reference variable a, making a an alias for i.

Think about it:How is this transfer method different from when using pointer variables as parameters?It can be found that using a reference type eliminates the need to declare that the parameter is a pointer variable in the swap function.The pointer variable needs to open up another memory unit,Its content is an address.The reference variable is not a separate variable,Does not occupy separate memory units,In the example, the data types of the variables a and b are the same as the actual parameters.They are all integers.

When calling the swap function in the main function, the actual parameter does not need to use the address of the variable (prefix the variable name with ampersand), but directly use the variable name.The system sends the actual parameter's address to the formal parameter instead of its value.

This transfer method is equivalent to "variable parameter" in pascal language. Obviously, this usage is simpler than using pointer variables. Using references to variables,Can partially replace the operation of the pointer.Some problems that could only be handled with pointers in the past,Can now be replaced by references,This reduces the difficulty of program design.

[Example] Sort the 3 variables in ascending order.

#include<iostream>
using namespace std;
int main ()
{
  void sort (int&, int&, int&);//function declaration,Formal parameter is a reference type
  int a, b, c;//a, b, c are variables to be sorted
  int a1, b1, c1;//the final value of a1, b1, c1 is the ordered sequence
  cout<"please enter 3 integers:";
  cin>>a>>b>>c;//Enter a, b, c
  a1=a;b1=b;c1=c;
  sort (a1, b1, c1);//Call the sort function, with a1, b1, c1 as arguments
  cout<"sorted order is"<a1<""<b1<""Good order
  return 0;
}
void sort (int & i, int & j, int & k) //sort i, j, k 3 numbers
{
  void change (int&, int&);//function declaration,Formal parameter is a reference type
  if (i>j) change (i, j);//make i<j
  if (i>k) change (i, k);//make i<k
  if (j>k) change (j, k);//make j<k
}
void change (int&x, int&y) //make x and y interchangeable
{
  int temp;
  temp=x;
  x=y;
  y=temp;
}

The operation is as follows:

please enter 3 integers:23 12 -345↙
sorted order is -345 12 23

You can see:this program is easy to understand,Not prone to errors.Because the virtual and real combination makes the formal parameters i, j, k a reference to the actual parameters a1, b1, c1 when calling the sort function, the i, j, k sorting is achieved by calling the function sort (a1, b1, c1) That is, sorting a1, b1, and c1 is implemented at the same time. Similarly, the execution of the change (i, j) function can realize the interchange of the actual parameters i and j.

References can be used not only for variables,Can also be used on objects.For example, the argument can be an object name,Pass the start address of the object when combining virtual and real.This will be introduced later.

When you see a form like&a,How to distinguish between declaring a reference variable or fetching an address?When a type precedes&a (such as int&a), it must be a declaration of a reference;If there is no type character in front (such as cout<< a), it is the address of the variable.

c
  • Previous jQuery focus map switching special effects code sharing
  • Next Explain enum types in C ++ and how to declare new types