Explanation of const reference in C++



As we all know, we can bind a reference to a const object and call it a reference to a constant. Unlike ordinary ordinary references, a constant reference cannot modify the object

to Note that it is not correct for a non-const reference to point to a constant object, but a constant reference can point to a non-constant object

such as:

JAVA; ">

const int c1 = 42;

JAVA;">

int i = 42;

int &r1 = c1;//Attempting to make a non-const reference point to a constant object will report an error

JAVA;"> ;

const int &r2 = i;//Correct, allows const int& to bind to a normal int

Guess you like

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