化合物类型,常量和自动C++

Compound types, const and auto in C++

本文关键字:C++ 常量 类型 化合物      更新时间:2023-10-16

我试图理解这段代码。我一直在弄清楚为什么de int*const int*.我可以使用一些帮助。

const int ci = i, &cr = ci;
auto b = ci; // b is an int (top-level const in ci is dropped)
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
auto d = &i; // d is an int*(& of an int object is int*)
auto e = &ci; // e is const int*(& of a const object is low-level const)

&i的意思是"取i的地址"。由于iint,因此&i的类型是int*。由于自动类型扣除规则,d的类型被推断为int*

同样的推理可以应用于ci。唯一的区别是const限定符。