Simple understanding of C++ references

Take a look at the following simple reference code:

#include<iostream>
using namespace std;
int main(){
    int x=12;
    int &a=x;//在此&仅仅表示的是标识作用 
    //**声明引用的时候,必须同时对其初始化操作。
    a=a+1;
    cout<<a<<endl;
}

Note:
the reference itself does not take up storage space, it is just an alias for the variable name.
A reference to an array cannot be created.
Pointers are accessed by addresses, while references are accessed by aliases.

int *p=&x;

References can be used as parameters. This is a large part of the role of references! ! !

The meaning of references
1) References exist as aliases for other variables, so they can replace pointers in some occasions
2) References are more readable and practical than pointers

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481348&siteId=291194637