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
The second version
MyClass c2();
is a function declaration - see the most vexing parse and gotw.
The first case is default initialisation.
The last case, new to C++11, will call the default constructor, if there is one, since even though it looks like an initialiser list {}
, it's empty.
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
.