C/C++ - const with Pointer or Reference
const
with Normal Variables
Two ways to add const
for normal variables:
|
Both means this variable cannot be assigned to another value.
For example,
|
The same error cames out for i
and j
:
const.cpp:9:4: error: cannot assign to variable 'i' with const-qualified type 'const int'
i = 2;
~ ^
const.cpp:7:12: note: variable 'i' declared const here
const int i = 1;
~~~~~~~~~~^~~~~
const.cpp:10:4: error: cannot assign to variable 'j' with const-qualified type 'const int'
j = 2;
~ ^
const.cpp:8:12: note: variable 'j' declared const here
int const j = 1;
~~~~~~~~~~^~~~~
2 errors generated.
const
and Reference
Also two ways to add const
to a reference:
|
Both have the same meaning.
There are two limits for them:
- This reference cannot be assigned to another variable
- The variable being referenced cannot change its value with this reference,
but it can change its value without using that reference.
For example,
|
constant reference can only be read.
if the value of the variable it referenced to has be changed,
it can only change the value without that reference.
const
and Pointer
This one is complicated.
But we can use the position of const
to remember which one const
is decorating:
|
For 1,const
decorates pNAME
,
that means pNAME
cannot be changed (No pNAME = ...
).
For 2,const
decorates *pNAME
,
so *pNAME
cannot be changed (No *pNAME = ...
).
For 3,const
decorates TYPE *pNAME
.
it is the same as 2,
saying that *pNAME
cannot be changed (No *pNAME = ...
).
For 4,const
decorates pNAME
and TYPE*
,
so both pNAME
and TYPE*
cannot be changed (No pNAME = ...
or *pNAME = ...
).
|