在成员变量之前放置成员函数有什么原因,反之亦然? [英] What reasons are there to place member functions before member variables or vice/versa?
问题描述
给定一个类,下面两种代码风格都有什么推理?
Given a class, what reasoning is there for either of the two following code styles?
Style A:
class Foo {
private:
doWork();
int bar;
}
Style B:
class Foo {
private:
int bar;
doWork();
}
对我来说,他们是一个领带。我喜欢样式A,因为成员变量感觉更细粒度,因此会出现过更常见的成员函数。但是,我也喜欢样式B,因为成员变量似乎以一种OOP风格的方式来确定类是什么表示。
For me, they are a tie. I like Style A because the member variables feel more fine-grained, and thus would appear past the more general member functions. However, I also like Style B, because the member variables seem to determine, in a OOP-style way, what the class is representing.
还有其他值得考虑的事情当在这两种样式之间进行选择时?
Are there other things worth considering when choosing between these two styles?
推荐答案
在C ++中,使用类来查看代码中的定义是很常见的,在头文件中。
In C++, it is common for those using the class to look at the definition in code, in the header file.
在Java中,使用该类查看从JavaDoc注释生成的文档是常见的(但不是通用的)。 IDE支持样式B.
In Java, it is common (but not universal) for those using the class to look at documentation generated from JavaDoc comments. IDEs support style B.
因此:
- 在C ++中,
这篇关于在成员变量之前放置成员函数有什么原因,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!