在声明时是否有新的C ++ 11成员初始化特性使初始化列表过时? [英] Has the new C++11 member initialization feature at declaration made initialization lists obsolete?

查看:139
本文介绍了在声明时是否有新的C ++ 11成员初始化特性使初始化列表过时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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)
{
    ...
}

C ++ 11成员初始化特性在声明时使初始化列表过时?如果不是,一个比另一个的优势是什么?什么情况会使初始化在声明有利,或初始化列表有利吗?什么时候应该使用另一个?

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?

推荐答案

不,他们不是过时的,因为这篇文章了解新的C ++ 11初始化表单类成员初始化 em>部分

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):


请注意,如果相同的数据成员同时有一个类成员初始化器和构造函数中的mem-init,后者优先。事实上,您可以通过以类成员初始化程序的形式为成员指定默认值来利用此行为,如果构造函数没有明确的初始化那个成员。否则,构造函数的mem-init将生效,覆盖类成员初始化器。 此技术在具有多个构造函数的类中非常有用

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

它不会删除对初始化列表的需要,但两个功能,而是一起工作,给你一个很好的方式来指定默认值,并在需要时覆盖它们。这似乎也是如何 Bjarne Stroustrup 看到它,他说:

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
};

并说:


hash_algorithm和s每个都有单个默认值的事实在代码混乱中丢失,并且在维护期间很容易成为一个问题。相反,我们可以忽略数据成员的初始化:

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
};

注意:C ++ 11中的缺点

在C ++ 11中的类成员初始化中使用有一个缺点,因为它使一个类成为非聚合,我们不能再使用聚合初始化,这可能相当令人惊讶。这不是在C ++ 14这种限制被删除的情况。有关详细信息,请参阅:具有非静态成员初始值设置的类的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成员初始化特性使初始化列表过时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆