我应该更喜欢私有成员函数还是未命名命名空间中的函数?

发布于 2024-09-14 07:06:58 字数 157 浏览 3 评论 0原文

我发现自己倾向于没有私有类函数。如果可能的话,我会将所有私有类函数的候选者放入未命名的命名空间中,并将所有必要的信息作为函数参数传递。我没有一个合理的解释为什么我要这样做,但至少它对我来说看起来更自然。因此,我需要在头文件中公开较少的内部细节。

您的意见是什么 - 这是正确的做法吗?

I've found myself that I tend not to have private class functions. If possible, all candidates to private class function rather I put in to unnamed namespace and pass all necessary information as function parameters. I don't have a sound explanation why I'm doing that but at least it looks more naturally to me. As a consequence I need to expose less internal details in the header file.

What is your opinion - is it correct practice?

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

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

发布评论

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

评论(4

或十年 2024-09-21 07:06:58

在我通常工作的半大型项目中(超过 200 万行代码),如果可以的话,我会禁止私有类函数。原因是私有类函数是私有的,但它在头文件中是可见的。这意味着,如果我以任何方式更改签名(或注释),有时我会得到完全重新编译的奖励,这需要几分钟(或几个小时,具体取决于项目)。

只需对此说“不”并将私有内容隐藏在 cpp 文件中即可。

如果我要重新开始一个大型 C++ 项目,我会强制执行 PIMPL Idiom: http://c2.com/cgi /wiki?PimplIdiom 将更多私人详细信息移至 cpp 文件中。

In the semi large projects where I usually work (more than 2 million lines of code) I would ban private class functions if I could. The reason being that a private class function is private but yet it's visible in the header file. This means if I change the signature (or the comment) in anyway I'm rewarded sometimes with a full recompile which costs several minutes (or hours depending on the project).

Just say no to that and hide what's private in the cpp file.

If I would start fresh on a large c++ project I would enforce PIMPL Idiom: http://c2.com/cgi/wiki?PimplIdiom to move even more private details into the cpp file.

顾忌 2024-09-21 07:06:58

我过去也这么做过,但结果总是很糟糕。您无法将类对象传递给函数,因为它们需要访问私有成员,大概是通过引用(或者最终会得到复杂的参数列表),因此您无法调用公共类方法。出于同样的原因,你也不能调用虚函数。我坚信(根据经验)这是一个坏主意。

底线:这听起来像是一种在实现“模块”对类具有某种特殊访问权限的情况下可能起作用的想法,但在 C++ 中情况并非如此。

I've done this in the past, and it has always ended badly. You cannot pass class objects to the functions, as they need to access the private members, presumably by reference (or you end up with convoluted parameter lists) so you cannot call public class methods. And you can't call virtual functions, for the same reason. I strongly believe (based on experience) that this is A Bad Idea.

Bottom line: This sounds like the kind of idea that might work where the implementation "module" has some special access to the class, but this is not the case in C++.

春夜浅 2024-09-21 07:06:58

它基本上归结为一个问题:所讨论的函数作为类的一部分是否真的有意义。如果您的唯一目的是将类的详细信息保留在标头之外,我会考虑使用 pimpl 习惯用法。

It basically comes down to a question of whether the function in question really makes sense as part of the class. If your only intent is to keep details of the class out of the header, I'd consider using the pimpl idiom instead.

紫南 2024-09-21 07:06:58

我认为这是一个很好的做法。它通常还具有隐藏辅助结构和数据类型的优点,从而减少了重建的频率和大小。如果发现这些功能在其他地方有用的话,它还可以使这些功能更容易地拆分到另一个模块中。

I think this is a good practice. It often has the benefit of hiding auxiallary structures and data types as well, which reduces the frequency and size of rebuilds. It also makes the functions easier to split out into another module if it turns out that they're useful elsewhere.

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