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*:

  1. class Foo { /* ... */ };
  2. void f(const Foo** p);
  3. void g(const Foo* const* p);
  4. int main()
  5. {
  6. Foo** p = /*...*/;
  7. // ...
  8. f(p); // ERROR: it's illegal and immoral to convert Foo** to const Foo**
  9. g(p); // Okay: it's legal and moral to convert Foo** to const Foo* const*
  10. // ...
  11. }

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:

  1. class Foo {
  2. public:
  3. void modify(); // make some modification to the this object
  4. };
  5. int main()
  6. {
  7. const Foo x;
  8. Foo* p;
  9. const Foo** q = &p; // q now points to p; this is (fortunately!) an error
  10. *q = &x; // p now points to x
  11. p->modify(); // Ouch: modifies a const Foo!!
  12. // ...
  13. }

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**?的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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: ...

  4. error: C2664: “zajiao::zajiao(const zajiao &)”: 无法将参数 1 从“const char [12]”转换为“char *”

    原本打算在QT用一个字符串"ABCDEF12345"作为类zajiao的构造函数的参数,用来创建类zajiao的对象zajiao1. zajiao zajiao1("AB ...

  5. ERROR C3848:具有类型"const XXX" 的表达式会丢失一些 const-volatile 限定符以调用"YYY" with"ZZZ"

    今天看书,Thinking in c++ volume 2 "Adaptable function objects" 里面作者说: Suppose, for example, th ...

  6. Data conversion error converting

    词错如果出现在sql语句中,那么多半是类型转换的问题

  7. 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 ...

  8. Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes

    http://blog.csdn.net/xx326664162/article/details/51859106 总算有个靠谱的了

  9. 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 ...

随机推荐

  1. SSM-配置文件标签随笔-概要

    xmlns: xmlns是web.xml文件用到的命名空间xmlns:xsi是指web.xml遵守xml规范xsi:schemaLocation是指具体用到的schema资源

  2. IE7浏览器下CSS属性选择器二三事

    一.为何专门说起IE7 以前,或者说数年前,我们从事桌面端网页开发的时候,基本上都还要兼顾IE6浏览器, 即使有些特性,IE7支持,我们也会忽略之.于是,我们会不自然地把IE6和IE7浏览器归为一路货 ...

  3. 《Java程序设计》第五周学习总结

    20145224 <Java程序设计>第五周学习总结 教材学习内容总结 第八章异常处理 8.1.1使用try.catch ·教材范例用户连续输入整数,输入0结束后显示输入数的平均值(代码如 ...

  4. $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法

    $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法 <input type='checkbox' id='cb'/>  ...

  5. javascript 函数调用方式

    1. 通过函数名直接调用 函数名(实际参数): 2. 通过指向函数的变量去调用 var 变量=函数名: 变量(实际参数):

  6. Binary Tree Level Order Traversal II [LeetCode]

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. ztree edit_super

    <SCRIPT type="text/javascript"> var setting = { view : { addHoverDom : addHoverDom, ...

  8. JDE报表开发笔记(R5537011 收货校验统计表)

    业务场景:根据批次收货,收货后对该批次产品进行检验,记录检验结果生成统计表. 涉及表:主表F37011,业务从表F43121/F4101/F4108 ------------------------- ...

  9. groupadd命令详解(实例)

     groupadd命令详解(实例)  1.作用groupadd命令用于将新组加入系统. 2.格式groupadd [-g gid] [-o]] [-r] [-f] groupname 3.主要参数-g ...

  10. 深入理解Redis:命令处理流程

    Redis是著名的NoSQL键值数据库服务器,为了保证效率,其数据都缓存在内存中.与Memcached相比,Redis支持的数据类型更多,包括String,List,Set,Zset和Hash.下面简 ...