Skip to main content

The understanding on the Reference (&) answers this question..

Reference is just an alias to the variable that it is assigned to it..

And const is a constraint imposed by the compiler to the variable that is declared as const

int x = 1000; const int &r = x;

int x = 1000; const int &r = x; 

In this case, its a const reference to a non const variable. So you cannot change the data of x with reference variable r(just acts a read only).. yet you can still change the data x by modifying x

const int z = 3000; int &t = z

const int z = 3000; int &t = z 

In this case, non const reference to const member which is meaningless. You are saying reference can allow you to edit a const member(which is never possible)..

So if you want to create a reference for a const member, it has to be like the first case you mentioned

const int z = 3000; const int &t = z;

const int z = 3000; const int &t = z; 

The understanding on the Reference (&) answers this question..

Reference is just an alias to the variable that it is assigned to it..

And const is a constraint imposed by the compiler to the variable that is declared as const

int x = 1000; const int &r = x;

In this case, its a const reference to a non const variable. So you cannot change the data of x with reference variable r(just acts a read only).. yet you can still change the data x by modifying x

const int z = 3000; int &t = z

In this case, non const reference to const member which is meaningless. You are saying reference can allow you to edit a const member(which is never possible)..

So if you want to create a reference for a const member, it has to be like the first case you mentioned

const int z = 3000; const int &t = z;

The understanding on the Reference (&) answers this question..

Reference is just an alias to the variable that it is assigned to it..

And const is a constraint imposed by the compiler to the variable that is declared as const

int x = 1000; const int &r = x; 

In this case, its a const reference to a non const variable. So you cannot change the data of x with reference variable r(just acts a read only).. yet you can still change the data x by modifying x

const int z = 3000; int &t = z 

In this case, non const reference to const member which is meaningless. You are saying reference can allow you to edit a const member(which is never possible)..

So if you want to create a reference for a const member, it has to be like the first case you mentioned

const int z = 3000; const int &t = z; 
Source Link
ravs2627
  • 871
  • 8
  • 3

The understanding on the Reference (&) answers this question..

Reference is just an alias to the variable that it is assigned to it..

And const is a constraint imposed by the compiler to the variable that is declared as const

int x = 1000; const int &r = x;

In this case, its a const reference to a non const variable. So you cannot change the data of x with reference variable r(just acts a read only).. yet you can still change the data x by modifying x

const int z = 3000; int &t = z

In this case, non const reference to const member which is meaningless. You are saying reference can allow you to edit a const member(which is never possible)..

So if you want to create a reference for a const member, it has to be like the first case you mentioned

const int z = 3000; const int &t = z;