|
|
|
|
|
|
|
|
|
|
Submitted by srchvrs on Wed, 06/14/2017 - 15:51
As we may remember, in C++ there are two types of constant pointers. The pointer of the first type (the most common one) can be changed, but not the memory it points to:const int * const_mem = ... ; *const_mem = 3; // compile error
The constant pointer of the second type is basically a reference and it cannot be changed, but you can still change respective memory:
int * const const_ptr = ... ; *const_ptr = 3; // fine! const_ptr++; // compile error
Of course, you can define a constant pointer to constant memory as well:const int * const const_ptr_mem = ... ; *const_ptr_mem = 3; // compile error! const const_ptr_mem++; // compile error
References, however, are constant by design. You can assign reference a value only once. You cannot change the reference value afterwards! Thus, references are basically constant non-null pointers. Turns out that you can still define a constant reference in C++:int const & const_ref = 3;
Well, why would such non-sense thing be possible? The answer is that it is not. C and C++ have an extremely quirky way of declaring complex types with complicated rules, which are applied basically in a inside-out right-to-left fashion. Thus, in the previous declaration const still applies to int rather than to int&. In other words, the latter declaration is equivalent to:const int & const_mem_ref = 3;
To declare a true constant reference, which is unsurprisingly illegal, you need to place the modifier const between the '&' and the variable name:int & const const_ref = 3;
Bottom line? Hopefully, reading this short note will help one reduce confusion in the future. As usual, simple illustration code is available.
|
|
|
|
|
|
|
|
|
|