http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html 写道
[18.17] Why am I getting an error converting a Foo** → Foo const**?
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
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.
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**.)
实际上 形参改为 类似 char const * const *的形式,C中也照样warning。C++中无warning
分享到:
相关推荐
第一章 关键字 1.1 最宽恒大量的关键字auto 1.2 最快的关键字register 1.3 最不实名的关键字static ...4.6 数组参数与指针参数 4.7 函数指针 第五章 内存管理 5.1 什么是野指针 5.2 栈堆静态区 5.3 常见内存错误与对策
def是一个二级指针,它指向的是一个一维数组的指针,数组的元素都是float. (2)double*(*gh)[10]; gh是一个指针,它指向一个一维数组,数组元素都是double*. (3)double(*f[10])(); f是一个数组,f有10个元素,元素都是...
12_二级指针做输入_第2种内存模型_课堂答疑_多维数组名本质抛砖 13_二级指针做输入_第3种内存模型_传智扫地僧 14_二级指针三种内存模型示意图_传智扫地僧 15_玩转多级指针_传智扫地僧 16_两个辅助指针变量挖字符串...
1.1.13 指向指针的指针(二级指针) 55 1.1.14 指向二维数组的指针 57 1.1.15 指针变量做为函数的参数 57 1.1.16 一维数组名作为函数参数 57 1.1.17 二维数组名作为函数参数 58 1.1.18 const关键字保护数组内容 58 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
7.5.2 用指向结构体变量的指针作为函数参数 7.5.3 返回结构体类型值的函数 7.6 共用体类型数据 7.6.1 共用体的特点 7.6.2 共用体变量的应用 7.7 枚举类型数据 7.8 用typedef定义类型 习题七 第8章 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 ...
6.3.2 用指针变量作函数参数接收数组地址 6.3.3 多维数组与指针 6.4 字符串与指针 6.5 函数与指针 6.5.1 用函数指针变量调用函数 6.5.2 用指向函数的指针作函数参数 6.6 返回指针值的函数 6.7 指针数组和指向指针的...
6.3.2 用指针变量作函数参数接收数组地址 6.3.3 多维数组与指针 6.4 字符串与指针 6.5 函数与指针 6.5.1 用函数指针变量调用函数 6.5.2 用指向函数的指针作函数参数 6.6 返回指针值的函数 6.7 指针数组和指向指针的...
4.9 能否用void ** 通用指针作为参数,使函数模拟按引用传递参数? 48 4.10 我有一个函数extern intf(int *); ,它接受指向int型的指针。我怎样用引用方式传入一个常数?调用f(&5);似乎不行。 49 4.11 C语言...
7.2.1指向const的指针 7.2.2const指针 7.2.3赋值和类型检查 7.3函数参数和返回值 7.3.1传递const值 7.3.2返回const值 7.3.3传递和返回地址 7.4类 7.4.1类里的const和enum 7.4.2编译期间类里的常量 7.4.3const对象和...