C++ 类型声明 - 指针星号位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2704167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type Declaration - Pointer Asterisk Position
提问by Feyyaz
in C++, the following means "allocate memory for an int pointer":
在 C++ 中,以下表示“为 int 指针分配内存”:
int* number;
So, the asterisk is part of the variable type; without it, that would mean "allocate memory for an int".
所以,星号是变量类型的一部分;没有它,这将意味着“为 int 分配内存”。
Then, wouldn't it make more sense if the following meant "allocate memory for two int pointers"?
那么,如果下面的意思是“为两个 int 指针分配内存”不是更有意义吗?
int* number1, number2;
回答by Kate Gregory
Stroustrup was asked this and he said (paraphrasing)
Stroustrup 被问到这个,他说(释义)
- if you think more C-ish you will say int *aand Employee *pE(so in your head you're thinking "the content of ais an integer")
- if you think more C++-ish you will say int* aand Employee* pE(so in your head it's "ais an integer pointer")
You can think however you like, as long as you never declare two pointers on the same line.
- 如果您想更多的C-ISH你会说INT *一个和员工*的pE(所以在你的头上,你会想“的内容一个是一个整数”)
- 如果你认为更多的 C++-ish 你会说int* a和Employee* pE(所以在你的脑海中它是“ a是一个整数指针”)
您可以随心所欲地思考,只要您永远不要在同一行上声明两个指针。
Works for me. I'm an Employee* pE
kind of person, but I'm married to an Employee *pE
kind of person - my advice would be not to get too worked up about it.
为我工作。我是一Employee* pE
类人,但我嫁给了一Employee *pE
类人——我的建议是不要太激动。
回答by kennytm
Actually the asterisk is attached to the variable (a convention inherited from C), so
实际上星号附加到变量(从 C 继承的约定),所以
int * number1, number2;
declares number1
as a pointer to int
(i.e. *number1
is an int
), and number2
as an int
.
声明number1
为指向int
(即*number1
是int
)的指针,并声明number2
为int
.
The spacing has no effect on how the number
's are typed. It just serves as a token separator. All of the following are the same to compiler, because the spaces will be stripped after parsing.
间距对number
's 的输入方式没有影响。它仅用作标记分隔符。以下所有对编译器来说都是一样的,因为解析后空格会被剥离。
int *a;
int*a;
int* a;
int * a;
int/**a***/*/*a***/a;
Use
用
int* number1, *number2;
to create two pointers, or even better, split it into multiple declarations to avoid confusion.
要创建两个指针,或者甚至更好,将其拆分为多个声明以避免混淆。
int* number1;
int* number2;
回答by AnT
You are oversimplifying the structure of C++ declaration (even though the points you make are perfectly logical). Only at the first sight it might seem that C++ declaration consists of type name and a sequence of comma separated entity names, but in reality in C++ (as well as in C) declaration actually consists of type name and sequence of declarators. The full type information for an entity you declare is split between twoseparate locations and portion of it is actually a part of its declarator. It is just the way it is in C++. In order to better reflect the actual structure of the declaration (as seen by the language), it might be a good idea to format declarations as
您过度简化了 C++ 声明的结构(即使您提出的观点完全合乎逻辑)。乍一看,C++ 声明似乎由类型名称和一系列逗号分隔的实体名称组成,但实际上在 C++ 中(以及在 C 中)声明实际上由类型名称和声明符序列组成。您声明的实体的完整类型信息分为两个单独的位置,其中一部分实际上是其声明符的一部分。这就是它在 C++ 中的方式。为了更好地反映声明的实际结构(如语言所见),将声明的格式设置为
int *a, *b;
i.e. explicitly group *
s with the entity names, not with the type name. (But in the end it is a matter of personal preference.)
即*
使用实体名称显式分组s,而不是使用类型名称。(但归根结底还是看个人喜好了。)
As for why it is designed that that way in the language, as you ask in one of the comments... As you know, the parts of the declaration syntax that describe the type of the entity being declared can appear on the left-hand side of the name (like *
and &
) as well as on its right-hand side (like ()
and []
) as in
至于为什么在语言中以这种方式设计,正如您在其中一条评论中所问的那样......如您所知,描述所声明实体类型的声明语法部分可以出现在左侧名称的一侧(如*
和&
)以及其右侧(如()
和[]
),如
int *f(), (&g)[5], h[2][2];
For the bits that appear on the right it is simply impossible to do it in any other way. They have no choice but to be grouped with the entity name, not with the type name. One might ask further why the above declarations are not done in C++ as
对于出现在右侧的位,根本不可能以任何其他方式做到这一点。它们别无选择,只能与实体名称分组,而不是与类型名称分组。有人可能会问为什么上面的声明不是在 C++ 中完成的
int *() f;
int (&)[5] g;
int [2][2] h;
(i.e. everything type-related appears on the left-hand side in a compact group)... Well, the answer to this question is just that it is the way it is in C++. The approach is inherited from C and there the "declaration must resemble the use" is often quoted as the rationale.
(即与类型相关的所有内容都出现在紧凑组的左侧)... 嗯,这个问题的答案只是它在 C++ 中的方式。该方法是从 C 继承而来的,并且“声明必须类似于使用”通常被引用为基本原理。
P.S. Another thing to remember (and what is often interpreted incorrectly) is that qualifiers adjacent to the type name in the declaration are part of the common type, not a part of the first individual declarator. For example
PS 要记住的另一件事(以及经常被错误解释的)是声明中与类型名称相邻的限定符是公共type的一部分,而不是第一个单独声明符的一部分。例如
int const *a, *b;
declares const int *a
and const int *b
, not int *b
as some might mistakingly believe. For this reason I personally prefer to use the more logical
声明const int *a
and const int *b
,不像int *b
有些人可能误认为的那样。出于这个原因,我个人更喜欢使用更合乎逻辑的
const int *a, *b;
ordering (even though at the same time I prefer to group *
with the name, not with the type).
排序(尽管同时我更喜欢*
按名称分组,而不是按类型分组)。
回答by sbi
This is just one of the many irregularities of C's declaration syntax. The type modifier *
is part of the type, yet syntactically it belongs to the identifier that's declared.
The same is true for &
and []
, BTW.
这只是 C 声明语法的众多违规行为之一。类型修饰符*
是类型的一部分,但在语法上它属于声明的标识符。和
也是如此,顺便说一句。&
[]
See herefor what *
does besides modifying a type.
请参阅此处了解*
除了修改类型之外还有什么作用。
回答by Eli Bendersky
No,
不,
int* number1, number2;
Declares number1
as a pointer to int
, and number2
as an int
. Personally, I also prefer attaching the asterisk to the type, which suffers from exactly the gotcha you bring up in this question, so I would declare two pointers as follows:
声明number1
作为指针int
,并number2
作为一个int
。就我个人而言,我也更喜欢将星号附加到类型上,这正是您在这个问题中提出的问题,因此我将声明两个指针如下:
int* number1;
int* number2;