问题描述
使用 C++11,我们现在可以在头声明中初始化类成员:
class aClass{私人的:int mint{100};上市:一类();~aClass();};
所以我有点困惑.传统上,构造函数中的初始化列表已用于成员初始化:
aClass::aClass():分钟(100){...}
声明时新的 C++11 成员初始化功能是否使初始化列表过时了?如果不是,一个比另一个有什么优势?什么情况会使声明时初始化或初始化列表有利?什么时候应该使用一个?
不,它们并没有像本文一样过时 了解新的 C++11 初始化形式在类成员初始化部分(强调我的)说:><块引用>
请记住,如果同一个数据成员在构造函数中同时具有类成员初始值设定项和 mem-init,则后者优先.事实上,您可以通过以类成员初始值设定项的形式为成员指定默认值来利用此行为,如果构造函数没有显式的 mem-init for那个成员.否则,构造函数的 mem-init 将生效,覆盖类成员初始值设定项.此技术在具有多个构造函数的类中很有用
因此,尽管在类成员中初始化是一个很好的便利,但它并没有消除对初始化列表的需要,而是两个功能一起工作,为您提供了一种指定默认值并在需要时覆盖它们的好方法.他说,这似乎也是 Bjarne Stroustrup 的看法:
<块引用>这节省了一些输入,但真正的好处来自具有多个构造函数的类.通常,所有构造函数都为一个成员使用一个通用的初始化器:
并提供具有公共初始化程序的成员示例:
class A {上市:A(): a(7), b(5), hash_algorithm("MD5"), s("Constructor run") {}A(int a_val) : a(a_val), b(5), hash_algorithm("MD5"), s("Constructor run") {}A(D d) : a(7), b(g(d)), hash_algorithm("MD5"), s("Constructor run") {}国际a, b;私人的:HashingFunction hash_algorithm;//应用于所有 A 实例的加密哈希std::string s;//指示对象生命周期状态的字符串};
并说:
<块引用>hash_algorithm 和 s 每个都有一个默认值这一事实在代码混乱中丢失了,并且很容易在维护期间成为问题.相反,我们可以分解数据成员的初始化:
class A {上市:A(): a(7), b(5) {}A(int a_val) : a(a_val), b(5) {}A(D d) : a(7), b(g(d)) {}国际a, b;私人的:HashingFunction hash_algorithm{"MD5"};//应用于所有 A 实例的加密哈希std::string s{"构造函数运行"};//指示对象生命周期状态的字符串};
注意:C++11 的缺点
在 C++11 中使用类成员初始化有一个缺点,因为它使类成为非聚合我们不能再使用 聚合初始化 这可能相当令人惊讶.在删除此限制的 C++14 中,情况并非如此.请参阅:具有非静态成员初始值设定项的类的 C++11 聚合初始化,了解更多详细信息.
With C++11, we now have the ability to initialize class members in a header declaration:
class aClass
{
private:
int mInt{100};
public:
aClass();
~aClass();
};
So I'm a bit confused. Traditionally initialization lists in constructors have been used for member initialization:
aClass::aClass()
: mInt(100)
{
...
}
Has the new C++11 member initialization feature at declaration made initialization lists obsolete? If not, what are the advantages of one over the other? What situations would make initialization at declaration advantageous, or initialization lists advantageous? When should one be used over the other?
No, they are not obsolete as this article Get to Know the New C++11 Initialization Forms says in the Class Member Initialization section (emphasis mine):
Bear in mind that if the same data member has both a class member initializer and a mem-init in the constructor, the latter takes precedence. In fact, you can take advantage of this behavior by specifying a default value for a member in the form of a class member initializer that will be used if the constructor doesn't have an explicit mem-init for that member. Otherwise, the constructor's mem-init will take effect, overriding the class member initializer. This technique is useful in classes that have multiple constructors
So although in class member initialization is a nice convenience it does not remove the need for initialization lists but both features instead work together to give you a nice way to specify default values and override them when needed. This seems to be also how Bjarne Stroustrup sees it too, he says:
This saves a bit of typing, but the real benefits come in classes with multiple constructors. Often, all constructors use a common initializer for a member:
and provides an example of members which have a common initializer:
class A {
public:
A(): a(7), b(5), hash_algorithm("MD5"), s("Constructor run") {}
A(int a_val) : a(a_val), b(5), hash_algorithm("MD5"), s("Constructor run") {}
A(D d) : a(7), b(g(d)), hash_algorithm("MD5"), s("Constructor run") {}
int a, b;
private:
HashingFunction hash_algorithm; // Cryptographic hash to be applied to all A instances
std::string s; // String indicating state in object lifecycle
};
and says:
The fact that hash_algorithm and s each has a single default is lost in the mess of code and could easily become a problem during maintenance. Instead, we can factor out the initialization of the data members:
class A {
public:
A(): a(7), b(5) {}
A(int a_val) : a(a_val), b(5) {}
A(D d) : a(7), b(g(d)) {}
int a, b;
private:
HashingFunction hash_algorithm{"MD5"}; // Cryptographic hash to be applied to all A instances
std::string s{"Constructor run"}; // String indicating state in object lifecycle
};
Note: disadvantage in C++11
There is one disadvantage to using in class member initialization in C++11 since it makes a class a non-aggregate we can no longer use aggregate initialization which may be rather surprising. This is not the case in C++14 where this restriction was removed. See: C++11 aggregate initialization for classes with non-static member initializers for more details.
这篇关于声明时新的 C++11 成员初始化功能是否使初始化列表过时了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!