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

前端 未结 4 1624
别跟我提以往
别跟我提以往 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 11:49

    Object obj();
    

    declares a function, not an object! This is an instance of the "Most Vexing Parse".

    Object obj = Object();
    

    requires Object to have an accessible move constructor or copy constructor (although the compiler might end up eliding the move/copy).

    Simple ways to just create an object include:

    Object obj;
    Object obj{};
    

提交回复
热议问题