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

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.

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值