Default initialization in C++

后端 未结 4 964
日久生厌
日久生厌 2020-12-04 19:54

I was asking myself something this morning, and I can\'t find the words to properly \"google\" for it:

Lets say I have:

struct Foo
{
  int bar;
};

s         


        
4条回答
  •  执笔经年
    2020-12-04 20:47

    For pod-types, the default initialization is zero-initialization.

    Therefore:

    Foo() : b() {} is the same as Foo() : b(0) {}

    I can't find the respective part of the C++ standard, but if you skip the initializer completely, then POD types shouldn't be default-initialized (unlike non-POD types, which are).

    Therefore in your case, only the third example is correctly initialized.

提交回复
热议问题