C++ 默认成员值最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11594846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default member values best practice
提问by Paul
Is it good practice when writing C++11 code to set default values for class members in the header file of the class?
在编写 C++11 代码以在类的头文件中为类成员设置默认值时,这是一种好习惯吗?
Or is it better to do this in the constructor of the class?
还是在类的构造函数中这样做更好?
EDIT:
编辑:
I mean:
我的意思是:
foo.h:
foo.h:
#include <string>
using std::string;
class Foo{
private:
string greet = "hello";
public:
Foo();
};
VS
VS
foo.cpp(of course with the necessary header file, but without the in-class initialization):
foo.cpp(当然有必要的头文件,但没有类内初始化):
Foo::Foo(){
greet = "hello";
}
Which one is better and why?
哪个更好,为什么?
回答by Kerrek SB
If a class member is always initialized with the same initial value, then you should make the initializer inline, so as to avoid duplication. If the initial value depends on the constructor, then put it in the constructor initializer list. (And never use assignment in the way you did.)
如果一个类成员总是用相同的初始值初始化,那么你应该使初始化器内联,以避免重复。如果初始值取决于构造函数,则将其放入构造函数初始值设定项列表中。(并且永远不要像你那样使用赋值。)
Example:
例子:
class Foo
{
bool done = false; // always start like this
int qty;
Bar * p;
public:
Foo() : qty(0), p(nullptr) { }
Foo(int q, Bar * bp) : qty(q), p(bp) { }
explicit Foo(char const * s) : qty(std::strlen(s)), p(new Bar(s)) { }
// ...
};
In this hypothetical example, the member done
is always required to start as false
, so it's best to write the initializer inline. The other two members, qty
and p
, can be initialized differently in each of three different constructors, so they are initialized inside the constructors' initializer lists.
在这个假设的例子中,成员done
总是需要以 as 开头false
,所以最好编写内联初始化程序。其他两个成员qty
andp
可以在三个不同的构造函数中的每一个中进行不同的初始化,因此它们在构造函数的初始化列表中进行初始化。
A curiosum: Note that providing an inline initializer prevents your class from having a trivial default constructor.
好奇心:请注意,提供内联初始值设定项可防止您的类具有简单的默认构造函数。
回答by Martin
It depends whether you need to stay compatible with older C++ compilers .When you are not using C++11 you have to initialize most members (all non-static) in the constructor. Further many people advocate to explicitly initialize every member even if this means explicitly calling the default ctor. Usually you should place implementation details in a cpp file not in the header file, thus an example would be
这取决于您是否需要与旧的 C++ 编译器保持兼容。当您不使用 C++11 时,您必须在构造函数中初始化大多数成员(全部为非静态)。此外,许多人主张显式初始化每个成员,即使这意味着显式调用默认构造函数。通常你应该将实现细节放在 cpp 文件中而不是头文件中,因此一个例子是
Example:
//foo.h
class Foo{
public:
Foo();
private:
std::vector<int> vect;
};
//foo.cpp
Foo::Foo():vect(){
}
In C++11 you have more choices and in class member initializer will become very handy, especially if you have several cors. Here is a good link for more information: http://www.stroustrup.com/C++11FAQ.html#member-init
在 C++11 中,您有更多选择,并且类成员初始化器将变得非常方便,尤其是当您有多个 cors 时。这是一个很好的链接以获取更多信息:http: //www.stroustrup.com/C++11FAQ.html#member-init
After Edit:According to your code you are using C++11. To my knowledge there is only few information on good practice concerning the new possibilities but IMHO In class member initializer are very handy to concentrate initialization in one place, which reduces complexity and typing
编辑后:根据您的代码,您使用的是 C++11。据我所知,关于新可能性的良好实践的信息很少,但恕我直言,类成员初始化器非常方便将初始化集中在一个地方,这降低了复杂性和输入
回答by Christopher Oezbek
Initializing in headers has the main advantages of keeping code more local and easy to understand. It saves also some typing.
在标头中初始化的主要优点是使代码更加本地化且易于理解。它还可以节省一些打字。
The main disadvantage, in my opinion, is the need to include more headers to get access to constructors. Simple forward declaration won't suffice, making compilation take longer.
在我看来,主要的缺点是需要包含更多的头文件来访问构造函数。简单的前向声明是不够的,编译需要更长的时间。