const关键字在变量声明中的定位意义 [英] Significance of const keyword positioning in variable declarations

查看:128
本文介绍了const关键字在变量声明中的定位意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定位

const

在Objective-C中声明变量时的

关键字,例如:

keyword when declaring a variable in Objective-C, for example:

extern const NSString * MY_CONSTANT;

extern NSString * const MY_CONSTANT;

在分配中使用第一个版本会产生有关指针目标类型的限定符"被丢弃的警告,因此我假设第二个版本可确保指针地址保持不变.不过,我将非常感谢您提供更明确的答案.提前非常感谢!

Using the first version in assignments produces warnings about "qualifiers from pointer target type" being discarded so I'm assuming that the second version ensures that the pointer address remains the same. I would really appreciate a more definitive answer though. Many thanks in advance!

推荐答案

在第一种情况下,您声明了一个指向不可变的const NSString对象的可变指针,而在第二种情况下,您是在声明了指向一个不可变对象的不可变指针.可变的NSString对象.

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.

记住这一点的一种简单方法是查看*的位置.左侧的所有内容均为"pointee"类型,右侧的所有内容均描述了指针的属性.

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.

这篇关于const关键字在变量声明中的定位意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆