【问题标题】:Compound types, const and auto in C++C++ 中的复合类型、const 和 auto
【发布时间】:2018-01-04 12:37:39
【问题描述】:

我正在尝试理解这段代码。我一直在弄清楚为什么deint*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)

【问题讨论】:

标签: c++ c++11 constants auto


【解决方案1】:

&i 表示“取i 的地址”。因为iint,所以&i 的类型是int*。由于automatic type deduction rulesd的类型推导出为int*

同样的推理也适用于ci。唯一的区别是const 限定符。

【讨论】:

  • 非常感谢,这很有意义!
  • @BiancaStan 如果此答案适合您,请单击答案分数周围的复选标记。这比说谢谢要好;)
猜你喜欢
  • 2013-04-14
  • 2021-09-23
  • 1970-01-01
  • 2012-05-29
  • 2016-08-02
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
相关资源
最近更新 更多