Significance of const keyword positioning in variable declarations

[亡魂溺海] 提交于 2019-11-30 05:02:08

In the first case, you are declaring a mutable pointer to an immutable const NSString object, whereas in the second case, you are declaring an immutable pointer to a mutable NSString object.

An easy way to remember this is to look at where the * is situated; everything to the left of it is the "pointee" type, and everything to the right of it describes the properties of the pointer.

Jay
extern const NSString * MY_CONSTANT; 

- Pointer is variable , but the data pointed by pointer is constant.

 extern NSString * const MY_CONSTANT; 

- Pointer constant , but the data pointed by pointer is not constant.

In general, const always applies to the token just before the const. In the second case, the const means that the pointer is a constant, not the thing pointed at. The exception is when the const appears before anything that can meaningfully be constant, as in your first example. In this case it applies to the first type, in this case NSString, so its equivalent to extern NSString const * MY_CONSTANT

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!