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

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

问题描述

我发现自己,我倾向于没有私人类函数。如果可能,所有候选人私人类函数,而不是我把未命名的命名空间,并传递所有必要的信息作为函数参数。我没有一个健全的解释为什么我这样做,但至少它看起来更自然地对我。因此,我需要在头文件中显示更少的内部细节。



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



只是说不,并隐藏cpp文件中的私有。



如果我将在一个大型c ++项目上开始新的项目,我将强制使用PIMPL Idiom: http://c2.com/cgi/wiki?PimplIdiom 将更多的私人详细信息移动到cpp文件中。


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?

解决方案

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.

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

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