gpt4 book ai didi

const 可以应用于类型吗?

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:15 25 4
gpt4 key购买 nike

我知道 const 限定符只影响一个对象而不影响它的类型:

例如:

// Only the elements of an array are constant not the array itself
const int a[5];

假设我有:

float *const p1; // 1
float const *p2; // 2

在上面的第一种情况下,是否意味着指针 type 是常量而不是对象 p1?不允许限定类型怎么会这样?

最佳答案

float *const p1; // 1

表示指针 p1 是常量,它不能(而不应该)指向另一个地址。
不是指针 type 是常量,而是该类型的实例 p1 是常量。

使用 Clockwise spiral rule 以便更好地理解这一点。

How can this be when the types aren't allowed to be qualified?

这是不正确的。事实上,qualifier 是 type qualifier(§6.7.3)

的缩写形式

关于const 可以应用于类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618279/

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