在 C++ 中声明指针的更好约定是什么? MyClass* ptr(或)MyClass *ptr?
在 C++ 中声明指针的更好约定是什么?
MyClass* ptr
(或)
MyClass *ptr
我发现第一个有意义,因为我想声明 MyClass 指针而不是 MyClass 并指定类型修饰符。但我看到很多书都推荐后面的约定。您能给出您所遵循的惯例背后的理由吗?
Which is the better convention of declaring pointers in C++?
MyClass* ptr
(or)
MyClass *ptr
I find the first one meaningful, because i feel like declaring MyClass pointer rather than a MyClass and specifying a type modifier. But i see a lot of books recommending the later convention. Can you give the rational behind the convention you follow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
约定
MyClass *ptr
背后的原理是*
属于单个变量,因此如果您有:MyClass *ptr1, ptr2
这将
ptr2
声明为MyClass
,不是MyClass*
。除非使用这种极其混乱的声明类型,否则这是一个风格问题。
The rational behind the convention
MyClass *ptr
is that the*
belongs to a single variable, so if you have:MyClass *ptr1, ptr2
This declares
ptr2
as aMyClass
, not aMyClass*
.Barring the use of this horribly confusing type of declaration, it is a matter of style.