如何组织类的成员函数?
How to organize a class's member functions?
我将"sets"放在构造函数之后,因为它与对象设置有关。我拆分了get(放入查询(和集合,但不确定这是否好。组织成员功能的最佳实践是什么?
怎么样?
class Foo
{
// Friends go here if it has
friend ...;
friend ...;
// First public, then protected and private
public:
// enums
enum {...}
// type defines.
typedef ...;
...
// Destructor and constructors
~Foo();
Foo(...);
Foo(...);
...
// Sets.
void setA(...);
void setB(...);
void setC(...);
...
// Inquiries (including gets).
A a() const;
B b() const;
...
// Operators.
void operator()(...);
...
// Operations.
void doSomething();
...
protected:
private:
};
很难判断,这取决于您的个人喜好或公司编码标准。通过查看您的代码,我可能不同意以下几点:
- 您的声明不是从
pubilc
订购的,"受保护"然后是私人的 - 当您在私人区域声明它们时,
friend
声明也有相同的努力。 所以我通常将它们放在私人部分,因此它在public section
中产生的噪音较小。
以下是我通常使用的申报顺序:
在类中使用指定的声明顺序:public
:在private
之前:,在数据成员(变量(之前的方法等。
类定义应该从它的public
:部分开始,然后是它的protected
:部分,然后是它的private
:部分。如果这些部分中的任何一个为空,请省略它们。
在每个部分中,声明通常应按以下顺序排列:
Typedefs and Enums
Constants (static const data members)
Constructors
Destructor
Methods, including static methods
Data Members (except static const data members)
好友声明应始终位于私有部分,禁用copy constructor
和其他操作员应位于私有:部分的末尾。这应该是课堂上的最后一件事。
相关文章:
- 如何使用指针传递给函数的数组中对象的函数成员
- c++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针