C ++风格:Stroustrup的指针星号位置 [英] C++ style: Stroustrup' s placement of pointer asterisks

查看:96
本文介绍了C ++风格:Stroustrup的指针星号位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么Stroustrup的风格是如下所示放置指针吗?具体来说,斯特鲁斯特鲁普(Stroustrup)提供了有关此事的指南?

Does anyone know why Stroustrup's style is the placement of pointers as follows? Specifically, what Stroustrup has provided for guidance about this matter?

int* p;

vs

int *p;

因为声明多个变量将需要在每个变量名旁边加上星号。这将导致:

because declaring multiple variables would require the asterisk next to each variable name. Which would result in:

int* p, *x;

vs

int *p, *x;

在K& RC书中,他们解释说星号/指针用作助记符理解。我觉得奇怪的是,指针/星号与类型相关联,而变量与每个示例的第二个示例所显示的一样。

In K&R C book, they explain that the asterisk/pointer is used as a mnemonic to aid in understanding. I find it odd that the pointer/asterisk is tied to the type, vs the variable as the second of each example shows. Interested if there is some background to why the first style is chosen.

希望从Stroustrup那里获得一些报价来做这个推理。

Hoping for some quote from Stroustrup in the reasoning for this.

我要在K& RC 2nd Edition语法中添加第235页,其中星号(指针)绑定到声明符(即标识符)上。

I'm adding in K&R C 2nd Edition grammar Page 235 where the asterisk (pointer) is tied to the declarator, which is an identifier.

ANSWER
在此 Stroustrup文章。他解释说,两者都是有效的,并且取决于程序员的喜好。

ANSWER In this article from Stroustrup on coding style. He explains that both are valid and it depends on programmer preference.

我不同意这是一个基于观点的问题。 Stroustrup的文章清楚地回答了这个问题,没有任何意见。

I disagree that this is an opinion based question. Stroustrup's article clearly answers the question without opinion.

推荐答案

C ++着重强调了类型指向指针声明,以避免任何形式的混淆,Bjarne建议-坚持每个声明一个指针

C++ emphasis heavily on types and when it comes to pointers declaration, to avoid any sort of confusion, Bjarne suggested - Stick to one pointer per declaration.

Bjarne Stroustrup的C ++样式和技术常见问题解答 [已添加重点]


int * p; 正确还是 int * p; 对吗?

在有效的C和C ++两者都具有完全相同的意义上,它们都是正确的。就语言定义和编译器而言,我们也可以说 int * p; int * p;

Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say int*p; or int * p;

int * p; int * p之间进行选择code>不是关于是非,而是关于风格和强调。 C强调表达;声明常常被认为仅仅是必要的邪恶。另一方面,C ++非常重视类型。

The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.

一个典型的C程序员编写 int * p; 并对其进行解释 * p是强调语法的int 的含义,并且可能指向C(和C ++)声明语法以证明样式的正确性。实际上, * 绑定到语法中的名称 p

A typical C programmer writes int *p; and explains it *p is what is the int emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

一个典型的C ++程序员编写 int * p; 并说明它 p是指向int 强调类型的指针。实际上, p 的类型是 int * 。我显然更喜欢这种强调,并认为它对于很好地使用C ++的更高级部分非常重要。

A typical C++ programmer writes int* p; and explains it p is a pointer to an int emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

(仅)当人们尝试使用单个声明:

The critical confusion comes (only) when people try to declare several pointers with a single declaration:

int * p,p1; //可能的错误:p1不是int *

* 放在更近的位置

int * p,p1; //可能的错误?

每个声明声明一个名称可以最大程度地减少问题-特别是在初始化变量时。人们书写的可能性大大降低:

Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:

int * p =& i;
int p1 = p; //错误:int初始化为int *

如果这样做,编译器会抱怨。

可以通过两种方式完成,有人会感到困惑。无论何时,只要有品位问题,讨论就会永远持续下去。 每个声明停留在一个指针上,并始终初始化变量,混乱的根源消失了。

And if they do, the compiler will complain.
Whenever something can be done in two ways, someone will be confused. Whenever something is a matter of taste, discussions can drag on forever. Stick to one pointer per declaration and always initialize variables and the source of confusion disappears.

有关C声明语法的详细讨论,请参见C ++的设计和演化。

See The Design and Evolution of C++ for a longer discussion of the C declaration syntax.

这篇关于C ++风格:Stroustrup的指针星号位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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