Why am I getting an error converting a ‘float**’ to ‘const float**’?
See Why am I getting an error converting a Foo** → const Foo**?
Because converting
Foo**
→const Foo**
would be invalid and dangerous ... The reason the conversion fromFoo**
→const Foo**
is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast
The reference goes on to give an example of how such an implicit conversion could allow me one to modify a const
object without a cast.
This is a very tricky restriction. It is related to the aliasing rules of the language. Take a look at what the standards say, because I have faced this once before:
(Page 61)
[Note: if a program could assign a pointer of type T** to a pointer of type const T** (that is, if line //1 below was allowed), a program could inadvertently modify a const object (as it is done on line //2). For example,
int main() { const char c = 'c'; char* pc; const char** pcc = &pc; //1: not allowed *pcc = &c; *pc = 'C'; //2: modifies a const object }
—end note]