1: References
Variable
Name
Space
Reference is not a variable
Reference is simply an alias of a variable
The reference does not have its own independent space
Reference to share space with the variable it references
The change to the reference is actually a change to the variable it refers to
References usually need to be initialized at the time of definition.
A reference cannot be re-directed to another variable once it is initialized
A const reference is a reference to a const
A non-const reference is not able to point to a const reference
A const reference can point to a normal object non-const variable
A reference to a const int type can point to a double type but will lose a value
#include <iostream>using namespace Std;int main () {int val = 10;int& Val_1 = val;cout<<val_1<<endl; return 0;}
A reference is passed by the reference operator "&" in front of the parameter when the function is defined.
For example: Swap (int &a,int &b);
Value-passing parameter cannot change the argument
Pointer passing parameters can change an argument
#include <iostream>using namespace std;void swap (int &a, int &b) {int temp = A; a=b;b=temp;} int main () {int a = 5; int b = 6;cout<< "a=" <<a<<endl;cout<< "b=" <<b<<endl;swap (A, b); cout<< "Change a=" <<a<<endl;cout<< "Change b=" <<b<<endl;return 0;}
2:const references
3: Reference Delivery
4: Reference as function return value
5: Reference differs from pointer
Pass-by-value is easy to understand, but changes in parameter values cannot affect actual parameters
Address transfer mode changes the corresponding arguments by changing the parameters, but the program is prone to error and difficult to read.
Any operation that references a parameter to a formal argument can change the data of the corresponding argument, making the function call appear convenient and natural
Another function of the reference is to return the referenced functions
A primary purpose of the function return reference is to place the function on the left side of the assignment operator.
Note: You cannot return a reference to a local variable.
A reference to access a variable is directly accessible, whereas a pointer is an indirect access.
A reference is an alias of a variable that itself does not allocate its own memory space alone, and the pointer has its own memory space.
Once the reference is initialized, no other variables can be referenced, and the pointer May.
Use references whenever possible, and use pointers when you have to.
Value passing arguments to the initial taxiing parameter to allocate space, copy the argument contents to the parameter
Non-allocating space when referencing the initial taxiing parameter of a passing argument
The pointer passing essence is the time to pass the initial taxiing parameter of an argument, and also to allocate space, allocating 4 bytes of space (32-bit machine!). If the 64-bit machine should be 8 bytes) If we want to change the pointer's address, simply passing the pointer is not possible.
C + + Learning Lesson02