私有类函数与未命名命名空间中的函数 [英] Private class functions vs Functions in unnamed namespace

查看:31
本文介绍了私有类函数与未命名命名空间中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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).

对此说不,并隐藏 cpp 文件中的私有内容.

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

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

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.

这篇关于私有类函数与未命名命名空间中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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