之前常量或const之后? [英] Const before or const after?

查看:123
本文介绍了之前常量或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;

这似乎无关紧要的唯一的事情就是你把其中星号侧常量关键字。我个人preFER把常量的类型左侧的指定它的数据是不可修改的,因为我觉得它读取更好的我的左到右的心态,但其语法是先?

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?

更重要的是为什么会出现指定常量数据,以及以何种风云二是正确的方法,你会preFER或需要一个比其他如果有的话?

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?

编辑:

所以它听起来像这是一个武断的决定时,编译器如何应在我出生前间preT事情起草长的标准。由于常量被应用到什么是对关键字的左边(默认?)我猜他们想通了。在增加的快捷键<无伤害/ EM >申请关键字,并通过分析A *或放类型,至少在其他方面预选赛,直到这样的时刻,报关变化; ...

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?

推荐答案

为什么会有你会preFER或需要一个比其他如有指定常量数据,以及以何种风云二正确的方法呢?

"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?"

从本质上讲,这常量说明符中的星号之前的位置并不重要的原因是,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编译器从解析输入左到右和成品加工的每个标记,因为它消耗的。消耗 * 标记改变当前声明的状态为指针类型。遇到常量 * 表示常量预选赛中被应用到一个指针声明;之前遇到它的 * 表示限定符应用于数据指出。

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.

因为如果常量预选赛的类型说明符之前或之后出现的语义不会改变,这是公认的两种方式。

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 *的功能1(无效)声明它返回一个函数无效* ,结果
无效(*函数2)(无效)声明了一个的函数指针的给它返回一个函数无效

void * function1(void) declares a function which returns 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.

这篇关于之前常量或const之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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