良好的编码:指针而不是引用?

发布于 2024-11-27 20:36:48 字数 365 浏览 0 评论 0原文

由于我编写 C/C++ 代码已有大约一年的时间,因此我尝试学习编写良好的 OO 和 C++ 代码的首选方法。这意味着,在编码时,我总是寻找实现某些内容的最佳方法,而不是仅仅输入一些内容并检查它是否有效。 我已经说过,在 C++ 中,在向函数传递参数时应该使用引用而不是原始指针,以使其更安全。所以这将被认为是更好的编码。尽管,在许多高质量的开源项目(C++)中,它们使用指针而不是引用。所有参数、返回值和其他内容都是指针,而不是更安全的引用。甚至 Microsoft 的 directx 也更多地使用指针而不是更安全的选项。我看不到使用指针而不是引用有任何好处,实际上我认为原始指针比引用更糟糕(当然在不需要动态分配的情况下)。

那么我的问题是:当有更好的方法可用时,为什么大多数开源项目都使用指针?

As I've been coding C/C++ for about a year now, I've tried to learn the preferred ways for writing good OO and C++ code. That means, when coding, I always look for the best ways when implementing something, rather than just typing something and checking if it works.
I've told that in C++ you should use references instead of raw pointers when passing arguments to functions, to make it more safe. So that would be considered as better coding. Although, in many high quality opensource projects(which are C++) they use pointers instead of references. All arguments, returns and stuff are pointers rather than safer references. Even Microsoft's directx uses pointers more than the safer option. I cannot see any benefits by using pointers instead of references, actually I think raw pointers are worse than references(in situations where dynamic alloc isn't needed of course).

So onto my question: why most of the opensource projects use pointers, when there are better ways available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

坏尐絯 2024-12-04 20:36:48

不确定这是否表明质量,但根据 Google 的 C++ 风格指南< /a>,

所有通过引用传递的参数都必须标记为 const。

...

事实上,Google 代码中有一个非常严格的约定,即输入
参数是值或常量引用,而输出参数是
指针。

Not sure if that's an indication of quality or not, but according to Google's C++ Style Guide,

All parameters passed by reference must be labeled const.

...

In fact it is a very strong convention in Google code that input
arguments are values or const references while output arguments are
pointers.

分开我的手 2024-12-04 20:36:48

在“许多高质量的开源项目”中,没有使用ideomatic C++。许多此类库将 C++ 视为添加了一些花哨功能的 C,而不是它自己的语言。

但是,如果没有特定的库,就很难说出使用引用与不使用引用的情况。如果您公开 C API,那么您当然被迫使用指针,因为 C 没有引用。

In "many high quality open source projects", ideomatic C++ is not used. Many such libraries view C++ as C with a few bells and whistles added, rather than as it's own language.

Without a specific library in question though, it's difficult to say anything about use of references versus no use of references. If you're exposing a C API, then of course you're forced to use pointers, because C does not have references.

歌入人心 2024-12-04 20:36:48

您认为完美的代码并非完美的原因有很多。

在开源领域,问题之一是有很多人免费贡献时间,他们的技能水平各不相同,所以你经常会得到各种风格的混合体,其中一些包括带类的 C。

在像 dx10 这样的大型项目中,使用指针和其他引用的原因有很多,但通常与使其尽可能容易地与“纯 C”集成有很大关系;它没有引用类型。或者只是一个封装在类抽象中的 C 库。

尽管如此,仅仅因为您看到周围的“糟糕”代码并不能成为您自己编写代码的理由。

There are many reasons why code that you think would be impeccable isn't.

In the Open source arena one of the problems is that there are many people contributing there time for free with different levels of skill, so you often end up with a mish mash of styles some of which include C with classes.

In large project like dx10 the are many reasons as to why pointers are used other reference but often has a lot to with making it as easy to integrate with 'pure C' as possible; which doesn't have reference types. Or is simply a C library wrapped in a class abstraction.

Non the less just because you see 'bad' code around you is no reason to write it yourself.

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