[英]Compound types, const and auto in C++
我試圖理解這段代碼。 我一直在弄清楚為什么d
和e
是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
的地址” 。 因為i
是一個int
,所以&i
的類型是int*
。 由於自動類型推導規則 , d
的類型被推導為int*
。
同樣的推理可以應用於ci
。 唯一的區別是const
限定符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.