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. laravel实现第三方登录

    https://github.com/laravel/socialite 这是官方的第三方登录包,支持很多国外的第三方登录 https://github.com/overtrue/socialite ...

  2. 【转】HTML, CSS和Javascript调试入门

    转 http://www.cnblogs.com/PurpleTide/archive/2011/11/25/2262269.html HTML, CSS和Javascript调试入门 本文介绍一些入 ...

  3. Loadrunner基础:Loadrunner Vuser基本概念和应用

    学习示例 Loadrunner自带有WebTour的网站可以帮助初学者学习性能测试安装完Loadrunner以后进入到Program Files下的WebTour文件加,启动WebTour服务在浏览器 ...

  4. Java:多线程,使用同步锁(Lock)时利用Condition类实现线程间通信

    如果程序不使用synchronized关键字来保证同步,而是直接使用Lock对象来保证同步,则系统中不存在隐式的同步监视器,也就不能用wait().notify().notifyAll()方法进行线程 ...

  5. SAP 快速报表

    快速报表,这个名字不知道是不是第一个用,不过以这种方式做的报表,速度确实挺快的,应该比QUERY快,还简单 T-CODE:SQVI 进入界面后,输入一个报表名称,点击新建,这时候可以选择,单表查询,链 ...

  6. [转]JDE910--jas.ini参数说明

    配置 jas.ini 文件 jas.ini 文件可以使用 Java™ Application Server (JAS) 来提供 JDE 安装的配置.您必须针对特定于每个环境的 JDE 实例来配置 ja ...

  7. c# 关键字delegate、event(委托与事件)[MSDN原文摘录][2]

    //Demo1:Declaring an event in an interface and implementing it in //a class. // event_keyword.cs usi ...

  8. HtmlHelper—DropDownList:SelectList、SelectListItem

    前言 在项目中经常使用到DropDownList来显示数据库中的数据,典型的例子为为某书籍选择所属类型. 使用SelectList来实现: 实现一: Controller 代码 SelectList ...

  9. js打印对象(object)

    function printObject(obj){//obj = {"cid":"C0","ctext":"区县"}; ...

  10. bzoj 1854: [Scoi2010]游戏

    #include<cstdio> #include<iostream> #include<cstring> #define M 2000008 using name ...