C ++ complains about converting 'int *** 'to 'const int **'
Recently when I was switching a project from C to C ++, I found that C ++ compilers do not allow converting int **
To const int **
, Even with explicit cast.
After searching on the web and thinking, I think the underlying reason is that C ++ does not allow converting const T *
To T *
, Unless const_cast
Is used. Let's assume that
Is of Type const
T **
, And B
Is of type T **. If we can assign the value of B
To
, Then we can make *
B
Point to a const t
Variable by dereferencing
, Without using const_cast
. This violates the principles of C ++.
One of the principles of C ++ is that, if a programmer uses const_cast
, He/she declares that the current conversion will do no harm to the programme; otherwise he/she may have not concerned about the possible danger brought by the current conversion, which is probably a bug. in the later case, the compiler must report an error to remind the programmer.