Small details about citations learned today

Now I am a freshman, and I hope to become a qualified programmer in the future. I just learned C++. I would like to start this blog to record my growth path. At the same time, I also hope that my blog will play the role of a memo, recording some small details encountered in the process of writing code.


Quote:

A reference to a variable is equivalent to the variable, which is equivalent to the variable's alias.

        int n=1;
        int &a=n;//a is a reference to n, and a is of type int &


Only variables can be referenced, constants and expressions cannot be referenced

        int n=1;

        int &a=n*2;//Compile error

        int &a=1;//Compile error


A reference can also be used as the return value of a function

        int &p()    {return n;}

This allows the function to be placed on the left side of the assignment operator, as in p()=1;


When defining a reference, add the const keyword in front of it, which is a constant reference

        int n=1;

        const int &a=n;

The key point is that you cannot modify the content of its reference through frequent references

Such as:

        int n=1;

        const int &a=n;

        a++;//Compile error

        n++;//n=2


A const reference can be initialized by a general reference, and a general reference cannot be initialized by a const reference

Such as:

int &a=const &n;//Compile error

const &a=int &n;//compiles correctly

Guess you like

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