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

  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.

时间: 2024-10-18 00:07:35

Why am I getting an error converting a Foo** → const Foo**?的相关文章

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

参考:Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes - Android studio 2.0 beta 6 问题: 在调试程序的时候,出现这个错误: Error:Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes Er

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

原本打算用一个字符串"ABCDEF12345"作为类zajiao的构造函数的参数,用来创建类zajiao的对象zajiao1. 1 zajiao zajiao1("ABCDEF12345"); 结果提示: F:\Users\denggelin\Documents\qtduojicheng\main.cpp:31: error: C2664: “zajiao::zajiao(const zajiao &)”: 无法将参数 1 从“const char [12]

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

今天看书,Thinking in c++ volume 2 "Adaptable function objects" 里面作者说: Suppose, for example, that we want to make the function object gt_n, definedearlier in this chapter, adaptable. All we need to do is the following: class gt_n : public unary_funct

error C2664: “FILE *fopen(const char *,const char *)”: 无法将参数 1 从“LPCTSTR”转换为“const char *”

遇到这个问题,请打开本项目的Properties(属性)-------> Configuration Properties(配置属性)-------->General(常规)------->Character Set(字符集)  把Use Unicode Character Set(使用Unicode字符集) 改成使用  Use Multi-byte Character  Set (使用多字节字符集).最后别忘了  应用------>确定.

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and

Google C++ Style Guide----英文版

转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51354326> Background C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power

重读C++ Primer笔记

C++ Primer 5E 有符号和无符号 无符号类型和有符号类型混合运算时,有符号数会被提升至无符号类型,如果其值为负责会产生错误. int main() { unsigned int u = 10; int i = -42; std::cout<<u+i<< std::endl; // 4294967264 if sizeof(int)==4 return 0; } 列表初始化 列表初始化过程不允许损失数据类型精度,所以下面代码中的两行无法通过编译 int main() { d

C++ Primer 学习笔记_54_类与数据抽象 --复制构造函数、赋值操作符

复制控制 --复制构造函数.赋值操作符 引言: 当定义一个新类型时,需要显式或隐式地指定复制.赋值和撤销该类型的对象时会发生什么– 复制构造函数.赋值操作符和析构函数的作用!      复制构造函数:具有单个形参,该形参(常用const修饰)是对该类类型的引用.当定义一个新对象并用一个同类型的对象对它进行初始化时,将显式的使用复制构造函数:当将该类型的对象传递给函数或者从函数返回该类型的对象时,将隐式使用复制构造函数.     析构函数:作为构造函数的互补,当对象超出作用域或动态分配的对象被删除

define() vs const 该如何选择?

使用 define(),除非考虑到可读性.类常量.或关注微优化 1.在 PHP 中是使用 define() 函数来定义常量,PHP 5.3.0 以后,PHP 中也能够使用 const 关键字来声明常量了,一个常量一旦被定义,就不能再改变或者取消定义 2.常量只能包含标量数据(boolean,integer,float 和 string).可以定义 resource 常量,但应尽量避免,因为会造成不可预料的结果 3.可以简单的通过指定其名字来取得常量的值,与变量不同,不应该在常量前面加上 $ 符号