When using the qualifier const as inthe case below:
const int value=67;
We are telling the compiler that we promise not to change the value of the variable "value". As such the compiler will merely replace instances of "value" with the actual value we initialized it with.since the compiler replaces each instance of the variable with its actual value, we must initialize it.
const int k; //error,must be initialized.
when bounding such a value to a reference, the reference must have such a qualifier to ensure we do not modify the value bound to through the reference(lvalue reference).
const int& c1=value;
c1=56; //error,we cannot modify it through the reference.
but we can also bind a non-const value to a const reference...
int i=89;
const int& ic=i; //const lvalue-ref
int& ir=i;
ir=7; //ok.ir is not const
ic=67990; //error ic is const
As for pointers:
const int val=90;
int* valp=&val;//error the pointer is not const.
const int* val2p=&val;//ok as they are both const
*val2p =788;//error as it is a pointer to non-writable space
However, a pointer to a const can be used to point to a non-const object.The idea is that pointers and references to const "think they point to or refer to const objects".
Unlike references, pointers are objects and can be made const. Meaning that a pointer can be made to point to a particular memory and not be allowed to point to another memory once initialized.
int* const x=&val;//it will point to that memory location only.
The fact that "x" is itself a pointer does not say anything about the value it points to.
The other interesting case is the "constexpr" case:
constexpr int* i=nullptr; //it is the pointer that is const.not the value it points to
constexpt const int* ii=&value; //both are const in this case.