关闭

C++不允许将int **转换为const int **

标签: c++compilerreportcweb
2053人阅读 评论(7) 收藏 举报
分类:

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 a  is of type const T ** , and b is of type T **. If we can assign the value of b to a , then we can make * b point to a const T variable by dereferencing a , 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.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:235108次
    • 积分:3206
    • 等级:
    • 排名:第11055名
    • 原创:90篇
    • 转载:5篇
    • 译文:1篇
    • 评论:166条
    最新评论