I cannot explain myself the following code:
double d = 100;
double const d1 = 30;
double* const p = &d; // Line 1
double* const p1 = &d1; // Line 2
In the above code, Line 1 is ok, but Line 2 produces the error:
"error C2440: 'initializing' : cannot convert from 'const double *__w64 ' to 'double *const '"
Can anyone elaborate on that, please?
(I am using VS C++ 2005, running on Win XP SP3)
The type
double* constis a const pointer to a non-const double. If you want a pointer to a const double, you have to usedouble const*(ordouble const* constif you want a const pointer to a const double).In C++, with a simple pointer to a double, you the const-ness of both the pointer itself (ie, can you make it point at another location) and the const-ness of the value (can you change the value through the pointer) can be configured independently. This gives you four very similar, but incompatibles types: