Difference between constructor calls with and without ( )

前端 未结 3 583
无人共我
无人共我 2021-02-05 06:46

I\'m a C++ beginner and would like to understand why

return std::list();

needs parentheses, but

std::list         


        
3条回答
  •  一个人的身影
    2021-02-05 07:27

    Both statements call default constructor.

    return std::list();
    

    This is same as:

    std::list value;
    return value;
    

    Here an object is created (using default constructor) and object is returned.

    std::list foo;
    

    Here object foo is created using the default constructor.

    Here are other way to do the same in C++11:

    std::list foo;
    std::list foo1{}; // C++11
    

提交回复
热议问题