关于免费静态函数使用的文章
我正在寻找一篇文章或文档来解释为什么当给定函数不修改或读取类的私有成员时,自由静态函数比私有成员函数更好。在我看来,优点是:
更少的依赖项
更好的封装(对于“猛犸象”类,了解至少那些函数调用不会修改成员是有帮助的)
我确信有人已经写了比我能做的更好的东西。
I am looking for an article or documentation that explains why a free static function is better than a private member function, when the given function does not modify or read the private members of the class. To my point of view the advantages are :
Less dependencies
Better encapsulation (for "mammoth" classes, it helps to know that at least those function calls do not modify the members)
I am certain someone has already written something better than what I can do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自由静态函数已经过时了。人们可以使用匿名命名空间中的函数来代替。
它们的可维护性稍高一些——因为它们无法访问私有成员,所以它们对实现细节的更改具有鲁棒性。静态成员函数也可以独立于实现细节,但编译器无法验证这一点。
Free static functions are passe. One would use a function in the anonymous namespace instead.
They are slightly more maintainable -- since they can't access private members, they are robust against changes to implementation details. A static member function could also be independent of implementation details, but you don't have the compiler verifying that.
如果您专门寻找一篇文章,您应该阅读 Effective C++ 作者:Scott Meyers。我建议这本书是所有认真的 C++ 程序员的必读书。
本主题的基本前提是,如果您可以使用类的现有接口方法实现自由函数,那么您应该这样做,因为这实际上简化了类。也就是说,能够看到类内部的代码越少,类的封装性就越好。添加更多的膨胀实际上会降低封装性。
If you are specifically looking for an article you should read "Item 23: Prefer non-member non-friend functions to member functions" in Effective C++ by Scott Meyers. I would suggest that this book is essential reading for all serious C++ programmers.
The basic premise for this topic is that if you can implement a free function using existing interface methods of a class then you should as this actually simplifies the class. That is, the less code that can see the internals of the class the more encapsulated it is. Adding more bloat actually decreases encapsulation.
您似乎误用了一些单词,因此我将尝试澄清
一般情况下,运算符重载往往会释放函数,以便在需要转换时可以在查找过程中找到它们。
当没有外部函数或类调用这些函数时,使用私有函数。
当静态函数不读取或写入 myclass,但它们是类概念的组成部分时,将使用静态函数。
常量函数可以用在 const 对象上,并且不修改类。
You seem to be misusing some words so I'll attempt to clarify
Generally, operator overloads tend to free functions so that way they can be found during lookup if a conversion is needed.
Private functions are used when no outside functions or classes will call those functions.
Static functions are used when they do not read or write to a myclass, but are an integeral part of the concept of the class.
Constant functions can be used on
const
objects, and do not modify the class.封装
是指将相关数据和功能组合起来。所以这里无关紧要。如果函数不更改成员变量,它会被标记为
const
,但根据上下文,它仍然可以愉快地属于类
,而不是被迫属于类
成为一个孤独的免费静态
函数。就我个人而言(这可以争论)我认为您应该将所有相关函数放在一个
类
中,或者至少是一个命名空间
,而不是将它们全部保留独自一人。虽然来自java,但从设计角度来看,这是一个有效的示例:http://download.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html,http://download.oracle.com/javase/ 1.5.0/docs/api/java/util/Collections.html
encapsulation
means combining relevant data and functions. So it's irrelevant here.If a function doesn't change member variables it is marked as
const
, but depending on the context it can still happily belong to aclass
instead of being forced to be a lonely freestatic
function.Personally (this can be debated) I believe that you should put together all relevant functions into a single
class
or AT THE VERY LEAST anamespace
instead of leaving them all alone.Though from java, from design pov this is a valid example: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html, http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html