Do the parentheses after the type name make a difference with new?

后端 未结 6 1069
余生分开走
余生分开走 2020-11-21 04:15

If \'Test\' is an ordinary class, is there any difference between:

Test* test = new Test;

and

Test* test = new Test();
         


        
6条回答
  •  无人共我
    2020-11-21 04:55

    In general we have default-initialization in first case and value-initialization in second case.

    For example: in case with int (POD type):

    • int* test = new int - we have any initialization and value of *test can be any.

    • int* test = new int() - *test will have 0 value.

    next behaviour depended from your type Test. We have defferent cases: Test have defult constructor, Test have generated default constructor, Test contain POD member, non POD member...

提交回复
热议问题