我是靠谱客的博主 满意便当,最近开发中收集的这篇文章主要介绍C++不允许将int **转换为const int **C++ Complains about Converting 'int **' to '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.

最后

以上就是满意便当为你收集整理的C++不允许将int **转换为const int **C++ Complains about Converting 'int **' to 'const int **' 的全部内容,希望文章能够帮你解决C++不允许将int **转换为const int **C++ Complains about Converting 'int **' to 'const int **' 所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(210)

评论列表共有 0 条评论

立即
投稿
返回
顶部