问题描述
首先,您可能知道 const
可用于使对象的数据或指针不可修改,或两者兼而有之.
To start you probably know that const
can be used to make either an object's data or a pointer not modifiable or both.
const Object* obj; // can't change data
Object* const obj; // can't change pointer
const Object* const obj; // can't change data or pointer
但是您也可以使用以下语法:
However you can also use the syntax:
Object const *obj; // same as const Object* obj;
似乎唯一重要的是您将 const
关键字放在星号的哪一侧.就我个人而言,我更喜欢将 const
放在类型的左侧以指定它的数据不可修改,因为我发现它在我从左到右的思维方式中读起来更好,但哪种语法先出现?
The only thing that seems to matter is which side of the asterisk you put the const
keyword. Personally I prefer to put const
on the left of the type to specify it's data is not modifiable as I find it reads better in my left-to-right mindset but which syntax came first?
更重要的是,为什么有两种正确的方式来指定 const
数据,以及在什么情况下您更喜欢或需要一种方式而不是另一种方式?
More importantly why is there two correct ways of specifying const
data and in what situation would you prefer or need one over the other if any?
所以这听起来像是一个武断的决定,因为编译器应该如何解释事物的标准早在我出生之前就已经起草好了.由于 const
应用于关键字左侧的内容(默认情况下?)我猜他们认为添加 shortcuts" 来应用关键字和至少在声明通过解析 * 或 & 更改之前以其他方式使用类型限定符....
So it sounds like this was an arbitrary decision when the standard for how compilers should interpret things was drafted long before I was born. Since const
is applied to what is to the left of the keyword (by default?) I guess they figured there was no harm in adding "shortcuts" to apply keywords and type qualifiers in other ways at least until such a time as the declaration changes by parsing a * or & ...
在 C 中也是如此,然后我假设?
This was the case in C as well then I'm assuming?
推荐答案
为什么有两种正确的方法来指定
const
数据,在什么情况下您更喜欢或需要一种方法(如果有)?
why is there two correct ways of specifying
const
data and in what situation would you prefer or need one over the other if any?
本质上,说明符中 const
在星号之前的位置无关紧要的原因是 C 语法是由 Kernighan 和 Ritchie 以这种方式定义的.
Essentially, the reason that the position of const
within specifiers prior to an asterisk does not matter is that the C grammar was defined that way by Kernighan and Ritchie.
他们以这种方式定义语法的原因可能是他们的 C 编译器从左到右解析输入,并在每个标记消耗它时完成处理.使用 *
标记会将当前声明的状态更改为指针类型.在*
之后遇到const
表示const
限定符应用于指针声明;在 *
之前遇到它意味着限定符应用于指向的数据.
The reason they defined the grammar in this way was likely that their C compiler parsed input from left-to-right and finished processing each token as it consumed that. Consuming the *
token changes the state of the current declaration to a pointer type. Encountering const
after *
means the const
qualifier is applied to a pointer declaration; encountering it prior to the *
means the qualifier is applied to the data pointed to.
因为如果 const
限定符出现在类型说明符之前或之后,语义不会改变,所以无论哪种方式都可以接受.
Because the semantic meaning does not change if the const
qualifier appears before or after the type specifiers, it is accepted either way.
在声明函数指针时会出现类似的情况,其中:
A similar sort of case arises when declaring function pointers, where:
void * function1(void)
声明一个返回void *
,
void * function1(void)
declares a function which returnsvoid *
,
void (* function2)(void)
声明一个函数指针,指向一个返回void
的函数.
void (* function2)(void)
declares a function pointer to a function which returns void
.
再次需要注意的是语言语法支持从左到右的解析器.
Again the thing to notice is that the language syntax supports a left-to-right parser.
这篇关于之前的常量还是之后的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!