简体   繁体   English

为什么这些成员没有被默认初始化?

[英]Why are these members not being default initialized?

In the following code, I get these warning messages: 在以下代码中,我得到了这些警告消息:

Member 'year' was not initialized in this constructor 未在此构造函数中初始化成员“ year”

Member 'month'was not initialized in this constructor 成员“ month”未在此构造函数中初始化

Member 'day' was not initialized in this constructor 成员“ day”未在此构造函数中初始化

... ...

However, I would expect the members to be default initialized to, say, 0 for the uint32_t types. 但是,我希望将uint32_t类型的成员默认初始化为0。

typedef uint32_t bmd2_uint32;

class UnitTestCInterface : public UnitTest {
{
  private:
    bmd2_uint32 year;
    bmd2_uint32 month;
    bmd2_uint32 day;
    ...

  public:
    UnitTestCInterface() 
      : filename(nullptr), UnitTest("UNIT TEST: C Interface") { };

How come these members aren't being default initialized, and what can I do about it? 这些成员为什么没有被默认初始化,我该怎么办?

You can initialize them using an initialization list: 您可以使用初始化列表来初始化它们:

UnitTestCInterface() 
      : year(0),month(0),day(0), filename(nullptr), UnitTest("UNIT TEST: C Interface")
     { };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 为什么成员没有默认初始化? - C++ Why the members are not default initialized? 默认情况下,C++ 结构的成员是否初始化为 0? - Are members of a C++ struct initialized to 0 by default? 错误C2512:没有合适的默认构造函数 - 为什么在构造函数中初始化属性? - Error C2512: no appropriate default constructor available - Why if properties are being initialized in constructor? 两个成员,默认在 Base 中初始化,可能在 Derived 中非默认初始化 - Two members, default initialized in Base, possibly non-default initialized in Derived 合成的默认构造函数如何初始化已经初始化的 class 成员? - How can the synthesized default constructor initialize already initialized class members? 为什么在 C++ 中未初始化数组中有随机数,而在半初始化数组的未初始化成员中没有? - Why there are Random numbers in non initialized array but not in non initialized members of half initialized array in C++? 为什么不能通过直接初始化语法初始化类数据成员? - Why class data members can't be initialized by direct initialization syntax? 为什么将全局变量和静态变量初始化为默认值? - Why are global and static variables initialized to their default values? 为什么默认情况下没有使用NULL初始化指针? - Why aren't pointers initialized with NULL by default? 为什么数据成员默认在C ++中是私有的? - Why are data members private by default in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM