A reference is to create a new alias for a certain variable, and the operation on the reference is the operation on its variable;
int &a=b; //Define reference a, which is the reference name of variable b, that is, it is also an alias
1. Reference method variable type +&+reference name = target variable name;
2. When declaring a reference, it must be initialized. Once initialized, it cannot point to other variables, nor can it point to a null value.
3. Declaring a reference is not a new definition of a variable. It is just a reference name and an alias of the variable (a reference name is only used as a reference to a variable). It will open up additional space and occupy memory! ! ! !
4. To find the address of a reference is to find the address of a variable (&a=&b)
#include<iostream>
using namespace std;
int main() {
int a = 1;
int b = 2;
int& p = a;
cout << &a << endl;
cout << &p << endl;
p = b;//引用对象没有改变只是把b的值赋给了a
cout << &b<< endl;
cout << &p << endl;
cout << a << endl;//a的值变成了2
/*p = nullptr;
cout << p << endl;*/
return 0;
}
Record it yourself, for reference only. If you have any questions, please leave a message~