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 x
p->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 ...
随机推荐
- .ecp认证文件(10.3版本)
arcgisserver,103,ecp.arcgis.engine,01-jan-2030,E9PJJE2G05FB8RZDF121 3dengine,103,ecp.arcgis.engine,0 ...
- [saiku] 通过 saiku 的 DEMO 分析 connection
示例:FOODMART connection: foodmart catalog: FoodMart schema: FoodMart cube: Sales/HR/Sales 2/.../ ==== ...
- thinkPHP开发基础知识 包括变量神马的
hinkPHP框架开发的应用程序,一般都采用单一入口的方式,下面是在应用首页文件中实现的定义: 1.在首页定义thinkPHP框架路径 2.定义项目名称及路径,一般项目名称与项目文件夹名称保持一致 3 ...
- hdu 1695 GCD(莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- FireFox 一键清理缓存
https://addons.mozilla.org/zh-CN/firefox/addon/empty-cache-button/
- PLSQL DEVELOPER 连接远程数据库 OCI客户端安装方法
安装使用过PLSQL Dev都知道,要连接数据库,必须配置TNS(Transparence Network Substrate),而直接安装PLSQL Dev 之后,本机是没有Oracle HOME的 ...
- HDU 3466
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- 在JSP中使用jQuery的冲突解决(收集整理)
在JSP中使用<jsp:include page="somethingPage.jsp"></jsp>来嵌套页面的时候,会出现jQuery之间的冲突 解决办 ...
- 第四章 CSS基础
1.CSS是cascading style sheets 层叠样式表.样式定义如何显示html元素,通常存储在样式表中,将样式添加到html中,是为了解决内容与表现分离的问题. 2.外部样式表可以极大 ...
- 胡扯两句——CDQ分治
之前听大神讲过CDQ分治大概是个什么东西,但是一直还没有真正去搞过.今天稍微看了一下,写点自己的理解. 首先CDQ分治有两个条件. 条件1:可以分成两个独立互不影响的问题(这里的"独立&qu ...