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

标签 c++ c++11

在 C++11 及更高版本中,这似乎是因为 default 之间的差异和 value初始化取决于我如何定义类,初始化的结果可能会有所不同。例如,查看下面的类或 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)并且默认构造就足够了,那么您不需要执行任何操作。

关于C++11:默认构造函数会导致类部分初始化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56727568/

相关文章:

c++ - 类的条件编译

c++ - 在 L1/L2 中快速合并 4K float 的排序子集

c++ - 如何从 QMetaObject::invokeMethod 获取返回值

c++ - gcc 4.9.1 不符合标准? (std::runtime_error)

c++ - 如何迭代到更小的容器中(即步幅!= 1)

c++ - move 构造函数不是继承的,也不是默认生成的

c++ - 统计数字和等于 x*m 的数字和的数字 x 的个数

C++ - 来自 std::string 的意外输出

C++11 线程卡在锁定互斥锁上

c++ - 更改 vector 中的值后,make_heap 无法按预期工作?