Why am I getting an error converting a Foo** → const Foo**?
Because converting Foo** → const Foo** would be invalid and dangerous.
C++ allows the (safe) conversion Foo* → Foo const*, but gives an error if you try to implicitly convert Foo** → const Foo**.
The rationale for why that error is a good thing is given below. But first, here is the most common solution: simply change const Foo** to const Foo* const*:
class Foo { /* ... */ };void f(const Foo** p);void g(const Foo* const* p);int main(){Foo** p = /*...*/;// ...f(p); // ERROR: it's illegal and immoral to convert Foo** to const Foo**g(p); // Okay: it's legal and moral to convert Foo** to const Foo* const*// ...}
The reason the conversion from Foo** → const Foo** is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast:
class Foo {public:void modify(); // make some modification to the this object};int main(){const Foo x;Foo* p;const Foo** q = &p; // q now points to p; this is (fortunately!) an error*q = &x; // p now points to xp->modify(); // Ouch: modifies a const Foo!!// ...}
If the q = &p line were legal, q would be pointing at p. The next line, *q = &x, changes p itself (since *q is p) to point at x. That would be a bad thing, since we would have lost the const qualifier: p is a Foo* but x is a const Foo. The p->modify() line exploits p’s ability to modify its referent, which is the real problem, since we ended up modifying a const Foo.
By way of analogy, if you hide a criminal under a lawful disguise, he can then exploit the trust given to that disguise. That’s bad.
Why am I getting an error converting a Foo** → const Foo**?的更多相关文章
- svn up出现类似svn: Error converting entry in directory '.' to UTF-8问题解决
执行svn up命令报错如下 # svn up svn: Error converting entry svn: Valid UTF- data (hex:) followed by invalid ...
- Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/lidroid/xutils/task/TaskHandler;
Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files defi ...
- error: converting to execution character set: Invalid or incomplete multibyte or wide character
交叉编译.c文件,遇到如下问题 arm-linux-gcc -o show_lines show_lines.c -lfreetype -lm show_lines.c:199:19: error: ...
- error: C2664: “zajiao::zajiao(const zajiao &)”: 无法将参数 1 从“const char [12]”转换为“char *”
原本打算在QT用一个字符串"ABCDEF12345"作为类zajiao的构造函数的参数,用来创建类zajiao的对象zajiao1. zajiao zajiao1("AB ...
- ERROR C3848:具有类型"const XXX" 的表达式会丢失一些 const-volatile 限定符以调用"YYY" with"ZZZ"
今天看书,Thinking in c++ volume 2 "Adaptable function objects" 里面作者说: Suppose, for example, th ...
- Data conversion error converting
词错如果出现在sql语句中,那么多半是类型转换的问题
- Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/adp;
Q:版本号不对,广告插件的版本号和项目中用的版本号不一致 A:adsplugins的build gradle里面用的版本号是10.0.1,修改app的build gradle 的google类都改成1 ...
- Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes
http://blog.csdn.net/xx326664162/article/details/51859106 总算有个靠谱的了
- android开发学习——Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/zxing/BarcodeFormat;
在Android Studio中,sync project没有错,但是run时会报错; http://blog.csdn.net/q568430333/article/details/50969033 ...
随机推荐
- linux笔记:搜索命令find,locate,which,whereis,grep
命令名称:find功能:文件搜索命令所在路径:/bin/find用法:find 搜索范围 匹配条件其他:举例:find /root -name initfind /root -size +1024fi ...
- Jsp-Servlet 那一大堆事儿--1
为毛全局变量声明时初始化在try内不能用? import javax.servlet.http .*; import java.io.*; import javax.servlet.*; import ...
- HTML4 和 HTML5 的10个关键区别
HTML5是HTML标准的下一个版本.越来越多的程序员开始HTML5来构建网站.如果你同时使用HTML4和HTML5的话 ,你会发现用HTML5从头构建,比从HTML4迁移到HTML5要方便很多.虽然 ...
- X-UniTMX:导入大型Tiled地图文件(*.tmx)到Unity3d中比较好的插件
因工作原因,需要导入格子数为1200x1200的Tiled地图文件(*.tmx)到Unity3d中显示出来.尝试过一些其它插件,后面发现X-UniTMX是比较好用的. X-UniTMXhttp://f ...
- [Spring] spring-session + JedisPool 实现 session 共享
1.至少导入四个jar包: jedis spring-session spring-data-redis commons-pool2 2.bean配置 <?xml version="1 ...
- BluetoothGatt API
punlic final class BluetoothGatt继承自Object , 实现了BluetoothProfile接口/** 相关的蓝牙协议可http://www.cnki.net/KCM ...
- border用处多
1. 使用border属性实现梯形 给定一个div,通过设定div四个边框不同的颜色且设置比较粗的边框线条,可以看到div除了中间的content部分,四个边框均成梯形状,既然已经有了梯形的雏形 ...
- MVC之超链接的寻址
传统式 href直接跟链接地址URL <a href="@Model.Base.BdtUrl" target="_blank">首页</a&g ...
- ABAP简单表维护的制作
为了知识的积累,特作了个简单的表维护. 因为自己之前做dynpro程序的时候建了一个Tree node的表,所以就不在此重复.(在表的交付和维护页签中标的属性要是‘允许标准表维护的’) 直接Alt+U ...
- State模式的经典应用场景:订单处理(c#实现)
State模式在对象内部状态发生变化的时候,改变自身的行为,这通常是通过切换内部状态对象实现的,对象将自身在各个状态的行为推给了状态对象,从而解开了行为与对象的依赖. 场景描述 在经典的订单处理场景中 ...