C ++ Programming Learning (j) references

   

Reference variable is an alias, that is to say, it is a name of another variable that already exists. Once the reference is initialized to a variable, you can use the reference name or the name of the variable to point to the variable.

Note the difference with the following pointers:

  • Null reference does not exist. Reference must be connected to a legitimate memory.
  • Once a reference is initialized to a target, it can not be directed to another object. Pointer can point to another object at any time.
  • Reference must be initialized when created. Pointer may be initialized at any time.

 

1, as a function of the parameters passed by reference

Look at the following:

#include <iostream>
using namespace std;
 
// function definition 
void the swap ( int X, int Y)
{
   int temp;
   TEMP = x; / * save the address values of x * / 
   x = y;     / * the assignment y to x * / 
   y = TEMP; / * the assignment x to y   * /
  
   return;
}
 
int main ()
{
   // local variable declaration 
   int A = 100 ;
    int B = 200 is ;
 
   COUT << " before the exchange, a value of: " << A << endl;
   COUT << " before the exchange, b values: " << B << endl;
 
   / * Call the function to exchange value * /
   swap(a, b);
 
   COUT << " After the exchange, a value of: " << A << endl;
   COUT << " After the exchange, b values: " << B << endl;
 
   return 0;
}
 

This time around the value of a, b constant exchange, why the obviously calling the swap () function, the value of ab has not changed? You would need to know the following:

(1, in the definition of the function, x, y is the parameter does not occupy memory declaration, the only argument is given only when the value assigned to parameter memory.

. (2, in the body of the function, parameter modifying any of the parameters are out of reach of the arguments parameter receives the value of the argument, will soon contact that is no argument:

Such as the above routine, a = 100, b = 200. After calling swap () function, x = 100, y = 200 (ab value to pass and after they have no xy contacted), this time in the swap () function, xy value exchange is completed. But it has nothing to do and ab, so the value of ab is still the same.

So how ab value change? See the code below:

#include <iostream>
using namespace std;
 
// function definition 
void the swap ( int & X, int & Y)
{
   int temp;
   TEMP = x; / * save the address values of x * / 
   x = y;     / * the assignment y to x * / 
   y = TEMP; / * the assignment x to y   * /
  
   return;
}
 
int main ()
{
   // local variable declaration 
   int A = 100 ;
    int B = 200 is ;
 
   COUT << " before the exchange, a value of: " << A << endl;
   COUT << " before the exchange, b values: " << B << endl;
 
   / * Call the function to exchange value * /
   swap(a, b);
 
   COUT << " After the exchange, a value of: " << A << endl;
   COUT << " After the exchange, b values: " << B << endl;
 
   return 0;
}

Xy added to the reference value corresponds to the quoted references from the individual names.

The above code example, from the individual to a name, called x; b from the individual to name, called y. Therefore, the process of exchanging xy values ​​in fact the exchange is ab.

 

2, the return value is incorporated

When the function returns a reference to an implicit return a pointer value is returned. In this way, it can function on the left side of an assignment statement. Similar Double & the setValues ( int I ). code show as below:

#include <iostream>
 
using namespace std;
 
Double vals [] = { 10.1 , 12.6 , 33.1 , 24.1 , 50.0 };
 
double& setValues( int i )
{
  return Vals [i];    // return a reference to the i-th element 
}
 
// To call the main function of the above-defined function 
int main ()
{
 
   COUT << " value before change " << endl;
    for ( int I = 0 ; I < . 5 ; I ++ )
   {
       cout << "vals[" << i << "] = ";
       cout << vals[i] << endl;
   }
 
   the setValues ( . 1 ) = 20.23 ; // change the second element of 
   the setValues ( . 3 ) = 70.8 ;   // change the fourth element 
 
   COUT << " value change " << endl;
    for ( int I = 0 ; I < . 5 ; I ++ )
   {
       cout << "vals[" << i << "] = ";
       cout << vals[i] << endl;
   }
   return 0;
}

The results obtained are:

Value before the change
false [ 0 ] = 10.1 
false [ 1 ] = 12.6 
false [ 2 ] = 33.1 
false [ 3 ] = 24.1 
false [ 4 ] = 50
The changed value
false [ 0 ] = 10.1 
false [ 1 ] = 20:23 
false [ 2 ] = 33.1 
false [ 3 ] = 70.8 
false [ 4 ] = 50

When returning a reference, pay attention to the referenced object can not exceed the scope. So to return a reference to a local variable is not legitimate, however, can return a reference to a static variable.

int & FUNC () {
    int Q;
    // return Q;! // error occurs at compile time 
   static  int X;
    return X;      // security, x in the function remains valid outside the scope 
}

Guess you like

Origin www.cnblogs.com/JuiceCat/p/12163102.html