When we pass a pointer as a parameter to a method, we actually pass a copy of the pointer to the method, or we can say that passing the pointer is the value passing of the pointer. The editor of the Error New Technology Channel will explain the details of the difference between references and pointers in C++. Come and have a look!
Detailed explanation of the difference between reference and pointer in C++
References are introduced from C++ and do not exist in C. In order to understand the concept of citation, you must first understand the definition of variables and the difference between references and variables. There are two elements of variables: name and space.
A reference is not a variable, it is just an alias for a variable, and it has no independent space. It only meets the "name" element of the variable, and the "space" element is not satisfied. In other words, a reference needs to share the same memory space as the variable it references, and the change to the reference is actually a modification to the variable referenced. And references must be initialized when defined.
The type of parameter passing and related points:
1 Pass by value: The value of the formal parameter cannot be modified. The actual parameter needs to initialize the formal parameter, and space needs to be allocated, and then copy the actual parameter content into the formal parameter.
2 Reference pass: There is no need to allocate space when the actual parameter is initialized.
3 Pointer passing: In essence, it is still passed by value. When the actual parameter is initialized, space still needs to be allocated. If you need to modify the address of the pointer, simply passing it with a pointer is not possible. You must use ** or *&
The following are the relevant contents quoted:
1 Quote concept
2 const quotes
3 Passing reference as parameter
4 Reference as function return value
5. The difference between reference and pointer
1 Quote concept
Just keep the following key points in mind:
(1) Reference is an alias for variables, and there is no independent space
(2) The reference needs to be shared with the variables it references
(3) Changes to a reference are actually changes to the variables it references
(4) References need to be initialized when defining
(5) Once the reference is initialized, other variables cannot be recited.
See the following example:
int main(void) { int val = 100; //int &refval;<span style="white-space:pre"> </span>//Error, the reference must be initialized int &refval = val; cout << "val="<< val << endl; refval = 200; <span style="white-space:pre"> </span>//In fact, the val variable has been changed cout << "val=" << val << endl; int val2 = 500; refval = val2; <span style="white-space:pre"> </span>//This does not mean that the variable val2 is referenced to val2, //Just only means assigning val2 to refval, that is, val. cout << "val=" << val << endl; return 0; }2 const quotes
The key points of const quote are as follows:
(1) As the name implies, a const reference is a reference to a const object.
(2) A const reference can refer to a non-const object, but a non-const reference cannot refer to a const object. See the following function example.
int a = 200; const int &ref = a; //OK const int b = 100; int &ref2 = b; //Error,
(3) The object value referenced by const is a constant and cannot be modified.
const int a = 200; const int &ref = a; ref = 100; //Error, the referenced object is a constant and cannot be modified
(4) A const reference can refer to variables of different types, but will produce a temporary variable. as follows:
double a = 3.14; const int &ref = a; //OK, the process it generates is: //int tmp = a; Generate the temporary variable tmp, and the data may be truncated. //const int &ref = tmp;The reference is actually a temporary variable
3 Passing reference as parameter
Passing a reference as a parameter is one of the parameters. It does not need to allocate memory space when the actual parameter is initialized. Here is an example:
void swap(int &a, int &b ) { int tmp = a; a = b; b = tmp; } int main() { int x = 20; int y = 30; swap(x, y ); cout << x << endl; //x = 30; cout << y << endl; //y = 20; }4 Reference as function return value
First list the examples, as follows:
int a[] = {1,2,3,4,5,6,7,8,9,0}; int &index(int i) { return a[i]; } int main() { index(3) = 100; cout << a[3] << endl; //output: 100 }This is another function of references, where the function can be placed to the left of the assignment operator. The index function returns a[]'s reference. Changes to the reference will modify the change itself, so a[3] is modified to 100.
5. The difference between reference and pointer
(1) Access reference is direct access, and access pointer is indirect access.
(2) Reference is an alias for variables. It does not allocate its own memory space separately, but the pointer has its own memory space
(3) Once the reference is initialized, no other variables can be referenced, but the pointer can
(4) Use references as much as possible and use pointers whenever you have to.
Thank you for reading and explaining the differences between references and pointers in C++ in detail. I hope it can help you. Thank you for your support from the new technology channel network!