C ++成员函数与自由函数 [英] C++ Member Functions vs Free Functions

查看:105
本文介绍了C ++成员函数与自由函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写程序时很多时候对这个设计决定感到困惑,但是我不能100%确定什么时候应该使一个函数成为类的成员函数,什么时候离开它作为一个正常的函数,其中其他源文件可以在函数声明在头文件中公开时调用函数。所需的函数成员变量的访问与大多数时候的决定有关吗?提前感谢。

I keep on getting confused about this design decision a lot of the time when I'm writing programs, but I'm not 100% sure when I should make a function to be a member function of a class, when to leave it as a normal function in which other source files can call the function when the function declaration is exposed in a header file. Does the desired access to member variables of a function have to do with the decision most of the time? Thanks in advance.

推荐答案

The Interface Principle by Herb Sutter

The Interface Principle by Herb Sutter

对于类X,所有函数,包括自由函数,都是

(a)提及X和

b)提供X

在逻辑上是X的一部分,因为它们构成X的接口的一部分。

有关深入讨论,请阅读Herb Sutter的命名空间和界面原则

For in depth discussion read Namespaces and the Interface Principle by Herb Sutter.

EDIT

实际上,如果你想了解C ++,阅读Herb Sutter写的一切:)

这篇关于C ++成员函数与自由函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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