C ++ 11:默认的默认构造函数可以导致部分初始化的类吗?

迈克·斯威尼(Mike Sweeney)

在C ++ 11及更高版本中,由于默认初始化之间的差异(取决于我如何定义类),似乎初始化的结果可能会有所不同。例如,查看下面的类或http://coliru.stacked-crooked.com/a/b45acc5acf847e73

#include <iostream>
#include <string>
#include <vector>

class ClassWithDefaultedConstructor {
 public:
  ClassWithDefaultedConstructor() = default;
  int GetInt() const { return member_int_; }
  bool GetBool() const { return member_bool_; }
  std::string GetString() const { return member_string_; }

 private:
  int member_int_;
  bool member_bool_;
  std::string member_string_;
  int member_int_array_[5];
};

class ClassWithUserProvidedDefaultConstructor {
 public:
  ClassWithUserProvidedDefaultConstructor() : member_int_() {}
  int GetInt() const { return member_int_; }
  bool GetBool() const { return member_bool_; }
  std::string GetString() const { return member_string_; }

 private:
  int member_int_;
  bool member_bool_;
  std::string member_string_;
  int member_int_array_[5];
};

class ClassWithDefaultedConstructorAndDefaultMemberInitializers {
 public:
  ClassWithDefaultedConstructorAndDefaultMemberInitializers() = default;
  int GetInt() const { return member_int_; }
  bool GetBool() const { return member_bool_; }
  std::string GetString() const { return member_string_; }

 private:
  int member_int_{};
  bool member_bool_{};
  std::string member_string_;
  int member_int_array_[5]{};
};

int main()
{
    std::cout << "Hello World!" << std::endl;

    // Default initialization: int and bool members will have indeterminate values
    ClassWithDefaultedConstructor default_init1;
    // Value initialization: int and bool members will be zero-initialized
    ClassWithDefaultedConstructor value_init1{};

    // Default initialization: member_int_ is value initialized to 0 in constructor
    // member initiazer list but member_bool_ and member_int_array_ have indeterminate values
    ClassWithUserProvidedDefaultConstructor default_init2;
    // Value initialization: member_bool_ and member_int_array_ are default initialized
    // and have indeterminate values
    ClassWithUserProvidedDefaultConstructor value_init2{};

    // Default initialization: int and bool members are value initialized to 0 because
    // of the default member initializers value initializing them
    ClassWithDefaultedConstructorAndDefaultMemberInitializers default_init3;
    // Value initialization: same as if no default member initializers were used
    ClassWithDefaultedConstructorAndDefaultMemberInitializers value_init3{};
}

因此,根据类的客​​户端选择声明对象(带有或不带有初始化程序)的方式,对象的起始状态将有所不同。这是真的?我在开源项目中遇到了很多代码,这些代码中标准库中类类型的成员(例如std::string未在用户提供的默认构造函数中初始化)。如果是这样,我似乎应该为所有成员提供默认的成员初始化程序,或者定义一个初始化所有成员的默认构造函数。缺省默认构造函数和对所有成员使用缺省成员初始化器与定义初始化成员初始化器列表中的所有成员的缺省构造器之间有什么区别吗?

轨道轻赛

= default 导致您的成员的默认初始化。

对于内置类型,没有什么

对于类类型,这是默认的构造函数。

如果您需要初始化某件事,请将其初始化。如果您不这样做,请不要。如果它是类类型的成员(例如std::string)并且默认构造已足够,则您无需执行任何操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++:具有初始化列表的默认构造函数

空的C ++ 11初始化程序列表导致容器中包含一个默认的构造对象

C ++初始化字段直接与默认构造函数中的初始化列表

C ++的类变量可以在没有Java构造函数的情况下初始化吗?

C ++ 11默认类成员与初始化器列表同时初始化

C ++默认构造函数未初始化指向nullptr的指针?

C ++初始化程序列表成员仍在调用默认构造函数?

没有默认构造函数的成员对象的 C++ 初始化:

C++ 默认成员初始化和构造函数

错误C2512:没有合适的默认构造函数-为什么在构造函数中初始化属性?

具有默认构造函数已删除的类类型可以默认初始化吗?

C ++默认参数可以用另一个参数初始化吗?

为什么 C++ 中类字段的默认初始化需要析构函数调用?

未初始化的局部变量,c ++ 11默认

是否考虑 new 关键字类中由默认构造函数初始化的元素也在 C++ 中使用 new 关键字?

C ++ 11:带“ = {}”的类内初始化不适用于显式构造函数

如何使用已删除的副本构造函数初始化类数组(C ++ 11)

可以使用C ++聚合初始化来构造实现接口的类的实例吗?

可以在C ++构造函数中初始化const成员吗?

C ++:默认在模板函数中初始化整数类型

C ++构造函数如何初始化其类的对象?

在类构造函数中初始化const向量(C ++)

c ++初始化模板类构造函数

C ++可以初始化基类的基类吗?

在C ++ 11中,我可以为非聚合类型实现像构造函数一样的聚合类型初始化吗?

我可以使用C ++ 11大括号初始化语法来避免为简单的聚合声明琐碎的构造函数吗?

C ++ 11使可变参数构造函数了解初始化列表的初始化列表

C++ 通过分配其他构造函数初始化的类成员来初始化类成员是一个坏主意吗?

继承:构造函数,在C ++ 11中初始化C之类的基类的数组成员