简体   繁体   中英

C++ Primer Lippman 5th exercise 2.27 b

C++ Primer exercise 2.27 5th ed.
Exercise: Which of the following initializations are legal? Explain why.

(b) int *const p2 = &i2;

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

I don't see where i2 has been declared? (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

int* ip, ip2;

not a pointer. So

 int *const p2 = &i2;

p2 is assigned the address of i2, a pointer. The const pointer of p2 does not come in to play unless you try and change the value of p2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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