为什么人们使用 Type *var 而不是 Type* var?
我搜索了,但我的关键字似乎不适合。我很确定这个问题已经被问过。所以,如果您知道链接或其他内容,请给我指出某个方向!
我的问题是,为什么这么多人使用
TypeIdentifier *varname;
而不是
TypeIdentifier* varname;
对我来说更符合逻辑,因为 *
修改类型而不是变量名称。
Possible Duplicate:
Declaring pointers; asterisk on the left or right of the space between the type and name?
Does it matter where I place the asterisk when declaring pointers in C++?
I searched, but my keywords don't seem to fit. I'm pretty sure this question has already been asked., so point me in some direction if you know a link or something !
My question is, why do so many people use
TypeIdentifier *varname;
Instead of
TypeIdentifier* varname;
which is much more logical to me as the *
modifies the type and not the variable name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为 C 的声明语法。
看起来好像定义了两个
int*
类型的变量,但实际上将a
声明为int*
和b
类型作为int
类型。如果你写这个就很明显了。然而,我个人更喜欢不写这样的定义,而是写这样的定义
,这样不会对类型产生任何疑问。
That's because of C's declaration syntax.
looks as if it defined two variables of type
int*
, but actually declaresa
as typeint*
andb
as typeint
. If you writethis is obvious. I personally however prefer to just not write such a definition, but instead write
which leaves no doubt about the types.
嗯,语言的语法却另有说法。您可以通过
这里会发生什么来轻松检查它?
Well, the syntax of language says otherwise. You can easily check it by
What will happen here?
C 约定是将
*
附加到变量上,因为一行中的多个变量不共享*
:在 C++ 中,规则是相同的,但是它由于上述声明的混乱性质,以及随后每个声明仅声明一个变量的做法,将疣附加到类型上更为常见:
我倾向于 C++ 约定,但也允许多个非指针或-一行引用声明:
The C convention is to attach the
*
to the variable, because multiple variables in a row don't share the*
:In C++, the rules are the same, but it is more common to attach the wart to the type, due to the confusing nature of the above declaration, and the consequent practice of declaring only one variable per declaration:
I lean towards the C++ convention, but also allowing multiple non-pointer-or-reference declarations on one line:
因为 K&R 使用了
Foo *bar
并且人们坚持使用它。Because K&R used
Foo *bar
and people stuck with it.在 C 和 C++ 中,声明语法(大部分)遵循用法。
该声明
可以读作“
n
is of typeint*
”,或“*n
is of typeint”。如果你看一下语言语法,就会发现后者更接近于它的解析方式。
对于仅由名称(例如
int
)和*
组成的类型,它不会产生很大的差异,但对于更复杂的声明来说很重要。通常的趋势是在 C 中使用
int *n;
,在 C++ 中使用int* n;
。后者是因为这是 Stroustrup 的个人偏好。我认为他的观点是“n
is of typeint*
”读取更自然,并且可以通过不编写复杂的声明来避免复杂性。例如,无论您喜欢哪种间距,最好都
写成
In both C and C++, the declaration syntax (mostly) follows usage.
The declaration
can be read as "
n
is of typeint*
", or as "*n
is of typeint
". And if you take a look at the language grammar, the latter corresponds more closely to the way it's parsed.For a type consisting of just a name (like
int
) and a*
, it doesn't make a whole lot of difference, but it matters for more complex declarations.The usual tendency is to use
int *n;
in C andint* n;
in C++. The latter is because that's Stroustrup's personal preference. His point, I think, is that the "n
is of typeint*
" reading is more natural, and the complexities can be avoided by not writing complex declarations. For example, whichever spacing you prefer, it's better to writethan either
or