将类中的所有双打初始化为零 [英] Initializing all doubles in a class to zero
问题描述
我有一个只有双成员变量的结构,例如
I have a struct that only has double member variables, e.g.
struct foo { double a, b, c; };
另一个类具有 std :: vector< foo> v
成员,该成员在初始化器列表中调用 std :: vector< foo> :: vector(size_t)
构造函数。
Another class has a std::vector<foo> v
member that calls the std::vector<foo>::vector(size_t)
constructor in the initializer list.
我想做的是为 foo
编写一个默认构造函数,以便将其中的所有双精度数初始化为零,而不必
What I'd like to do is write a default constructor for foo
so that all the doubles in it are initialized to zero, without having to write
foo(): a(0), b(0), c(0) { }
我一直需要向 foo
添加更多变量让它们成为 std :: array< double>
之类的容器的元素没有意义,因为它们都具有不同的用途。
I keep needing to add more variables to foo
, but it doesn't make sense to have them be elements of a container like std::array<double>
, because they all serve distinct purposes.
推荐答案
由于已将其标记为C ++ 14,因此可以像这样初始化成员变量,而不必在构造函数中对其进行初始化:
Since you've tagged this as C++14, you can initialize member variables like this without having to initialize them in a constructor:
struct foo {
double a = 0.0;
double b = 0.0;
double c = 0.0;
};
这篇关于将类中的所有双打初始化为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!