C++ 声明指针;类型和名称之间的空格左边还是右边的星号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2660633/
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
Declaring pointers; asterisk on the left or right of the space between the type and name?
提问by jakogut
Possible Duplicates:
What makes more sense - char* string or char *string?Pointer declarations in C++: placement of the asterisk
I've seen mixed versions of this in a lot of code. (This applies to C and C++, by the way.) People seem to declare pointers in one of two ways, and I have no idea which one is correct, of if it even matters.
我已经在很多代码中看到了这个的混合版本。(顺便说一下,这适用于 C 和 C++。)人们似乎以两种方式之一声明指针,我不知道哪一种是正确的,甚至不知道它是否重要。
The first way it to put the asterisk adjacent the type name, like so:
第一种方法是将星号放在类型名称旁边,如下所示:
someType* somePtr;
The second way is to put the asterisk adjacent the name of the variable, like so:
第二种方法是将星号放在变量名称旁边,如下所示:
someType *somePtr;
This has been driving me nuts for some time now. Is there any standard way of declaring pointers? Does it even matter how pointers are declared? I've used both declarations before, and I know that the compiler doesn't care which way it is. However, the fact that I've seen pointers declared in two different ways leads me to believe that there's a reason behind it. I'm curious if either method is more readable or logical in some way that I'm missing.
这已经让我发疯了一段时间了。是否有任何标准的方法来声明指针?指针的声明方式是否重要?我之前使用过这两个声明,我知道编译器并不关心它是哪种方式。然而,我已经看到以两种不同方式声明的指针这一事实让我相信这背后是有原因的。我很好奇这两种方法是否以某种我缺少的方式更具可读性或逻辑性。
采纳答案by Tyler McHenry
It's a matter of preference, and somewhat of a holy war, just like brace style.
这是一个偏好问题,有点像一场圣战,就像括号风格一样。
The style
样式
someType* somePtr;
is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr
is pointer-to-someType
".
强调指针变量的类型。它本质上是说,“类型somePtr
是指向-的someType
”。
The style
样式
someType *somePtr
is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr
is someType
".
强调指向数据的类型。它本质上是说,“指向的数据类型somePtr
是someType
”。
They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.
它们都意味着相同的事情,但这取决于给定程序员在创建指针时的心理模型是否“集中”,可以说,是指向指向的数据还是指针变量。
Putting it in the middle (as someType * somePtr
) is trying to avoid committing to either one.
把它放在中间 (as someType * somePtr
) 试图避免承诺任何一个。
回答by Johannes Schaub - litb
It doesn't matter. Someone will now come along and close the question as a dupe, and someone else will show how the int* a
way breaks if you declare multiple variables in the same declarations while the int *a
way better reflects the syntactical structure of the code, and another guy will show that Stroustrup prefers the int* a
way.
没关系。现在有人会过来把这个问题作为一个骗子来结束,int* a
如果你在同一个声明中声明多个变量,其他人会展示这种方式是如何打破的,而这种int *a
方式更好地反映了代码的句法结构,另一个人会展示 Stroustrup更喜欢的int* a
方式。
Many opinions, but no "right" way here.
许多意见,但这里没有“正确”的方式。
回答by Brian R. Bondy
It doesn't matter, it is personal preference.
没关系,看个人喜好。
Some people like to keep the type together:
有些人喜欢将类型保持在一起:
int* p;
Other people say that it should go next to the variable because of the following:
其他人说它应该放在变量旁边,原因如下:
int *p, x;//declare 1 int pointer and 1 int
int *p, *x;//declare 2 int pointers.
Over time you will just overlook this and accept both variations.
随着时间的推移,您将忽略这一点并接受这两种变化。
回答by Lance Diduck
The difference arose because C++ added a stronger type system on top of C. A C programmer usually thinks in terms of "values," so
之所以产生差异,是因为 C++ 在 C 之上添加了一个更强大的类型系统。 AC 程序员通常从“值”的角度思考,所以
int *pValue;
reads "the dereference of pValue is an int" whereas a C++ programmer thinks in "types" so
读取“pValue 的取消引用是一个 int”,而 C++ 程序员认为“类型”所以
int* pValue;
reads "the type pValue is pointer to int" The compiler sees no difference at all of course. However you will find that it is the C programmer who insist on "value semantics" when programming in C++.
读取“类型 pValue 是指向 int 的指针”当然编译器根本看不到任何区别。然而你会发现,在用 C++ 编程时,坚持“值语义”的是 C 程序员。
回答by dteoh
I think putting the asterisk adjacent to the name of the variable is clearer.
我认为将星号放在变量名称旁边更清晰。
You might mistakenly declare someType* one, two;
thinking that they are both pointers but only the variable one
is a pointer; two
is just a someType
. Declaring as someType *one, *two
avoids this problem.
你可能会错误地someType* one, two;
认为它们都是指针,但只有变量one
是指针;two
只是一个someType
. 声明为someType *one, *two
避免了这个问题。
回答by Driss Zouak
Every single way I've seen ever is
我见过的每一种方式都是
TheType *myPointer
because you are declaring a POINTER of type TheType. Similar declaring
因为您要声明 TheType 类型的 POINTER。类似的声明
TheType myVar
would be declaring an instance variable of type TheType.
将声明一个 TheType 类型的实例变量。
Also you can then clearly do this and have it easily readable
您也可以清楚地执行此操作并使其易于阅读
TheType myVar, *myPointer;