Why am I getting an error converting a Foo** → Foo const**?
来源:互联网 发布:匿名飞控源码 编辑:程序博客网 时间:2024/05/13 21:57
Because converting Foo** → Foo const** would be invalid and dangerous.
C++ allows the (safe) conversion Foo* → Foo const*, but gives an error if you try to implicitly convert Foo** → Foo const**.
The rationale for why that error is a good thing is given below. But first, here is the most common solution: simply change Foo const** to Foo const* const*:
class Foo { /* ... */ };void f(Foo const** p);void g(Foo const* const* p);int main(){ Foo** p = /*...*/; ... f(p); // ERROR: it's illegal and immoral to convert Foo** to Foo const** g(p); // OK: it's legal and moral to convert Foo** to Foo const* const* ...}The reason the conversion from Foo** → Foo const** 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; Foo const** 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 aFoo* 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.
Thankfully C++ prevents you from doing this: the line q = &p is flagged by the C++ compiler as a compile-time error. Reminder: please do not pointer-cast your way around that compile-time error message. Just Say No!
(Note: there is a conceptual similarity between this and the prohibition against converting Derived** to Base**.)
From:http://www.parashift.com/c++-faq/constptrptr-conversion.html
It is correct that a double **
cannot be implicitly converted to a const double **
. It can be converted to a const double * const *
, though.
Imagine this scenario:
const double cd = 7.0;double d = 4.0;double *pd = &d;double **ppd = &pd;const double **ppCd = ppd; //this is illegal, but if it were possible:*ppCd = &cd; //now *ppCd, which is also *ppd, which is pd, points to cd*pd = 3.14; // pd now points to cd and thus modifies a const value!
So, if your function does not intend to modify any of the pointers involved, change it to take a const double * const *
. If it intends to do modifications, you must decide whether all the modifications it does are safe and thus const_cast
can be used, or whether you really need to pass in a const double **
.
- Why am I getting an error converting a Foo** → Foo const**?
- foo
- foo ?
- foo
- [lucene异常]why am I getting a TooManyClause exception
- for(foo('A');foo('B')&&(i<2);foo('C')) ,其实考的就是for (初始化语句; 判断条件; 循环条件)
- Why am I getting an AttributeError when trying to print out(转载)
- Why why why why why am i a developer?
- Why am i a number?
- Smack “No Response From Server”. Not sure why am i getting this error
- Why am I getting this error “Expected resource of type raw” in Android Studio?
- 关于“Foo”
- foo学习
- Foo 是什么意思
- foo是什么意思
- Foo 是什么意思
- Foo 是什么意思
- foo是什么意思??
- ecshop批量添加商品到购物车----之三步走
- Wince LoadLibrary失败,GetLastError= 193
- 艺术编程
- 2014年百度之星程序设计大赛 - 资格赛 第四题 Labyrinth
- Android中关于JNI 的学习(二)对于JNI方法名,数据类型和方法签名的一些认识
- Why am I getting an error converting a Foo** → Foo const**?
- SNMPv3基于视图的访问控制模型VACM
- HDU 1208 DP || 记忆化搜索
- [BASIC-17] 矩阵乘法
- Uboot启动分析笔记-----Stage1(start.S与lowlevel_init.S详解)
- (八)贪心算法
- android插件化-apkplug中以监听方式获取OSGI服务-09
- ISO给UIImageView增加点击事件
- SNMPv3基于用户的安全模型USM(1)