开发者

Default initialization in C++

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

struct Foo2
{
   int bar;
   Foo2() {}
};

struct Foo3
{
   int bar;
   Foo3() : bar(0) {}
};

Now if I default instantiate Foo, Foo2 and Foo3:

Foo foo;
Foo2 foo2;
Foo3 foo3;

In which case(s) is the bar member properly initialized ?

(Well Foo3 obviously explicitely initialize it and is only showed here to explicit the difference with Foo2 so the question is mainly about the first two.)

Thank you ! :)


Only foo3 will be in all contexts. foo2 and foo will be if they are of static duration. Note that objects of type Foo may be zero initialized in other contexts:

Foo* foo = new Foo(); // will initialize bar to 0
Foo* foox = new Foo; // will not initialize bar to 0

while Foo2 will not:

Foo2* foo = new Foo2(); // will not initialize bar to 0
Foo2* foox = new Foo2; // will not initialize bar to 0

that area is tricky, the wording as changed between C++98 and C++03 and, IIRC, again with C++0X, so I'd not depend on it.

With

struct Foo4
{
   int bar;
   Foo4() : bar() {}
};

bar will always be initialized as well.


Since bar is a built-in type its default initialization will be undefined for Foo1 and Foo2. If it would have been a custom type, then the default constructor would have been called, but here it's not the case.

Lesson: always initialize your variables.


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.


Case 3 is the proper way, with a member initialization list.

None of the first two will be properly initialized, since you don't give them an initial value (exactly like a variable only defined is not initialized).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜