C++ reference in detail
- 2020-10-23 20:14:05
- OfStack
The reference variable is 1 individual name, that is, it is another name for an existing variable. Once a reference is initialized to a variable, the reference name or variable name can be used to point to the variable.
C++ refers to the vs pointer
References are easily confused with Pointers, and there are three major differences between them:
There is no empty reference. References must be connected to a valid block of memory. Once a reference is initialized to one object, it cannot be pointed to another. A pointer can point to another object at any time. References must be initialized at creation time. Pointers can be initialized at any time.Create a reference in C++
Imagine that a variable name is a tag attached to a variable in a memory location, and you can think of a reference as a second tag attached to a variable in a memory location. Therefore, you can access the contents of a variable by its original name or reference. Such as:
int i = 17;
We can declare reference variables for i as follows:
int& r = i;
double& s = d;
In these statements, & Read as a quote. Thus, the first statement can be read as "r is an integer reference initialized to i ", and the second statement can be read as "s is an double reference initialized to d ". The following examples use int and double references:
#include <iostream>
using namespace std;
int main ()
{
// Declare simple variables
int i;
double d;
// Declare reference variables
int& r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
[
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
References are commonly used for function argument lists and function return values. Here are two important concepts that C++ programmers must be aware of in relation to C++ references:
Take the reference as an argument
C++ supports passing references to functions as arguments, which is safer than passing 1-like arguments.
#include <iostream>
using namespace std;
// Function declaration
void swap(int& x, int& y);
int main ()
{
// Local variable declaration
int a = 100;
int b = 200;
cout << " Exchange of before, a Value: " << a << endl;
cout << " Exchange of before, b Value: " << b << endl;
/* Call functions to exchange values */
swap(a, b);
cout << " After exchanging, a Value: " << a << endl;
cout << " After exchanging, b Value: " << b << endl;
return 0;
}
// The function definitions
void swap(int& x, int& y)
{
int temp;
temp = x; /* Save the address x The value of the */
x = y; /* the y Assigned to x */
y = temp; /* the x Assigned to y */
return;
}
When the above code is compiled and executed, it produces the following results:
[
Before switching, the value of a: 100
Value of b before switching: 200
After swapping, the value of a: 200
After swapping, the value of b: 100
Take the reference as the return value
You can return a reference from the C++ function just as you would return any other data type 1. Using a reference instead of a pointer makes the C++ program easier to read and maintain. The C++ function can return 1 reference in a similar way to returning 1 pointer.
When a function returns a reference, an implicit pointer to the return value is returned. This way, the function can be placed to the left of the assignment statement. For example, look at this simple program:
#include <iostream>
using namespace std;
double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
double& setValues( int i )
{
return vals[i]; // Returns the first i A reference to an element
}
// To call the main function that defined the function above
int main ()
{
cout << " Change the value before " << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
setValues(1) = 20.23; // Change the first 2 An element
setValues(3) = 70.8; // Change the first 4 An element
cout << " The changed value " << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following results:
[
Change the value before
vals[0] = 10.1
vals[1] = 12.6
vals[2] = 33.1
vals[3] = 24.1
vals[4] = 50
The changed value
vals[0] = 10.1
vals[1] = 20.23
vals[2] = 33.1
vals[3] = 70.8
vals[4] = 50
When returning a reference, be careful that the referenced object does not go out of scope. It is illegal to return a reference to a local variable, but you can return a reference to a static variable.
int& func() {
int q;
//! return q; // An error occurred at compile time
static int x;
return x; // Security, x It is still valid outside the scope of the function
}
The above is the detailed content of C++ citation, more information about C++ citation please pay attention to other related articles on this site!