What's difference between Object obj = Object() and Object obj()?

前端 未结 4 1587
别跟我提以往
别跟我提以往 2020-12-25 11:17

I think that it has a lot of information about it but I don\'t know how this is called. I cannot understand difference between next two strings of code:

Obje         


        
4条回答
  •  攒了一身酷
    2020-12-25 12:02

    Object obj();
    

    Declares a function names obj that returns a Object and takes no parameters. This is generally not what you want.

    Object obj = Object();
    

    Declares an Object named obj then is copy initialized with a temporary default constructed Object. You normally don't want to do this either unless you are resetting an object back to a default state.

    Generally if you want to construct without calling a constructor you can use

    Object obj;
    //or
    Object obj{};
    

提交回复
热议问题