C++11 Difference in Constructors (Braces)

前端 未结 2 2025
忘了有多久
忘了有多久 2021-01-19 19:02

I am quite new to C++ and have observed, that the following lines of code act differently

MyClass c1;
c1.do_work() //works
MyClass c2();
c2.do_work() //compi         


        
2条回答
  •  攒了一身酷
    2021-01-19 19:46

    Ways one and three call the default constructor.

    MyClass c3{};
    

    Is a new initialization syntax called uniform initialization. This is called default brace initialization. However:

    MyClass c2();
    

    Declares a function c2 which takes no parameters with the return type of MyClass.

提交回复
热议问题