You may have used code such as [1]:
// Success
Here we obtain a pointer b
to the same object that a
points to, but we say that it is now immutable (through b, at least).
However, C++ does not allow a programmer to add const
ness more than one layer of indirection away [2]:
// error: invalid conversion from 'int' to 'const int'
Of course, you can still make the pointer itself const
[3]:
// Success
So it turns out that you can add const
ness to the pointer [3], or to the immediate pointee [1], but you can't add const
ness to the pointee of the pointee [2].
The question is: why not? How does adding const
ness violate const
-correctness? Answers on a postcard, please.
Update: As Kniht correctly pointed out in the comments (and, in fact, as the standard itself explains in a note), this is invalid for very good reason:
include
int main() { char* p = 0;
//char const a = &p; // not allowed, but let's pretend it is char const a = (char const**)&p; // instead force the cast to compile
char const orig = "original"; a = orig; // type of a is char const, which is the type of orig, this is allowed
assert(p == orig); // oops! char points to a char const }