C++函数中的指针
我知道从技术上讲,以下三种方法都是有效的,但是是否有任何逻辑理由以一种或另一种方式这样做?我的意思是,C++ 中的很多东西都是“技术上有效的”,但这并没有让它们变得不那么愚蠢。
int* someFunction(int* input)
{
// code
}
或者
int *someFunction(int *input)
{
// code
}
或者
int * someFunction(int * input)
{
// code
}
我个人认为第三种方法很烦人,但是有没有“正确”的方法?我通常更倾向于使用第一个(因为第二个看起来更像是被用作解引用运算符 - 但事实并非如此)
I know that technically all three ways below are valid, but is there any logical reason to do it one way or the other? I mean, lots of things in c++ are "technically valid" but that doesn't make them any less foolish.
int* someFunction(int* input)
{
// code
}
or
int *someFunction(int *input)
{
// code
}
or
int * someFunction(int * input)
{
// code
}
I personally think the third one is annoying, but is there a "correct" way? I am typically more inclined to use the first one (as the second looks more like it's being used as the dereference operator - which it isn't)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是个人品味的问题。我更喜欢第一种方法,而老派程序员则倾向于使用第二种方法(来自古老的 C 时代)。
对于差异,请考虑以下事项:
第一种方式 (
int* p
) 的吸引力在于它读作“int 指针是p
的类型”,而另一种int *p
读作“int 是*p
的一种类型”(这也是正确的)。It's a question of personal taste. I prefer the 1st approach, whereas the old-school programmers tend to use the 2nd (coming from the old good C times).
For a difference, consider the following:
The attractiveness of the first way (
int* p
) is that it reads as "int pointer is a type ofp
", whereas the alternateint *p
reads as "int is a type of*p
" (which is also correct).一切都是等价的。选择最适合您的口味。只要确保无论您选择哪一个,您都会在每种情况下应用该选择。星星和大括号放在哪里远不如每次将它们放在同一个地方重要。
就我个人而言,我更喜欢
int* someFunction(int* input);
,但谁在乎呢?All are equivalent. Choose the flavor that suits you best. Just be sure whichever you chose, you apply that choice in every case. Where your stars and curly braces go is far less important than putting them in the same place every time.
Personally, I prefer
int* someFunction(int* input);
, but who cares?我个人使用第二个选项,因为
int *p1, p2;
比int* p1, p2;
或int * p1, p2 更美观且更不易混淆;
。与函数的返回类型相同以保持相同的样式。这是个人的事情,没有什么“好”或“坏”的方法。
I personally use the second option, because
int *p1, p2;
is better looking and less confusing thanint* p1, p2;
orint * p1, p2;
. The same with functions' return type to keep the same style.It's a personal thing, there isn't any 'good' or 'bad' way.
一切都是等价的。我喜欢第三个,因为它使
*
脱颖而出。其他人则不同。如果您正在与其他人一起开发一个项目,请使用既定的风格。如果没有,请决定您自己的风格。
All are equivalent. I like the third because it makes the
*
stand out. Other people differ.If you're working on a project with others, use the established style. If not, decide on your own style.
不存在“一种正确的方法”。这完全取决于个人喜好。
第一种方法通常与在一个声明中使用多个声明符不兼容,因此使用它的人通常不会在声明中使用多个声明符。
但是第一种方法有它的支持者。
There's no "one correct way". It is all a matter of personal preference.
The first approach is not generally compatible with having multiple declarators in one declaration, so people who use it usually don't use more than one declarator in a declaration
Yet the first approach has its supporters.
我更喜欢第二种,原因在我的 上一篇中解释过回答。
*someFunction(someInput)
的类型为 int。编辑:Kernigan 和 Ritchie 肯定打算第二种。例如,请参阅 这段来自白色圣经的片段。
I prefer the second, for the reasons explained in my previous answer.
*someFunction(someInput)
has type int.EDIT: Kernigan and Ritchie definitely intended the second. See for instance this snippet from the white bible.