在 C++ 中声明指针的更好约定是什么? MyClass* ptr(或)MyClass *ptr?

发布于 2024-09-14 14:10:43 字数 205 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-09-21 14:10:43

约定 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 a MyClass, not a MyClass*.

Barring the use of this horribly confusing type of declaration, it is a matter of style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文