gpt4 book ai didi

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:50 30 4
gpt4 key购买 nike

将非 const 指针转换为 const 指针是合法的。

那为什么将指向非const的指针转换为指向const的指针是不合法的呢?

例如,为什么下面的代码是非法的:

char *s1 = 0;
const char *s2 = s1; // OK...
char *a[MAX]; // aka char **
const char **ps = a; // error!

最佳答案

来自标准:

const char c = 'c';
char* pc;
const char** pcc = &pc; // not allowed
*pcc = &c;
*pc = 'C'; // would allow to modify a const object

关于c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29240009/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com