Default Initialization Versus Zero Initialization

前端 未结 4 565
Happy的楠姐
Happy的楠姐 2020-12-20 05:14

I cannot understand the behavior of gcc 4.8.1 or Visual Studio 2015 with respect to default initialization versus value initialization.

It doesn\'t help that I\'m tr

4条回答
  •  时光说笑
    2020-12-20 05:48

    Foo foo;
    

    This default-initializes foo, and since Foo's default constructor is trivial, it effectively doesn't initialize it at all, so foo._bar can hold any value (including 0).

    Foo()
    

    This value-initializes the temporary object, which in case of trivial default constructor means zero-initialization, so Foo()._bar is equal to 0.

提交回复
热议问题