简体   繁体   English

C ++ Primer Lippman第五次练习2.27 b

[英]C++ Primer Lippman 5th exercise 2.27 b

C++ Primer exercise 2.27 5th ed. C ++入门练习2.27第5版。
Exercise: Which of the following initializations are legal? 练习:以下哪些初始化是合法的? Explain why. 解释为什么。

(b) int *const p2 = &i2; (b) int *const p2 = &i2;

This is legal according to: https://github.com/Mooophy/Cpp-Primer/tree/master/ch02#exercise-227 这是合法的: https : //github.com/Mooophy/Cpp-Primer/tree/master/ch02#exercise-227

I don't see where i2 has been declared? 我看不到在哪里声明了i2 (I've looked at the errata also.) (我也看过勘误表。)

It is exploiting the fact that the * goes with the name, not the type. 它利用*加上名称而不是类型的事实。

i2 is declared as an integer by the question above this one i2被上面那个问题声明为整数

int* ip, ip2;

not a pointer. 不是指针。 So 所以

 int *const p2 = &i2;

p2 is assigned the address of i2, a pointer. p2被分配了指针i2的地址。 The const pointer of p2 does not come in to play unless you try and change the value of p2. 除非您尝试更改p2的值,否则p2的const指针不会出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM