Search code examples
c++constructorcopy-constructormost-vexing-parse

Why copy constructor not getting called in this case


Say, I have a class A

Now when I am doing

A a(A()); 

what exactly happens?


Solution

  • Despite appearances, A a(A()); is not an object definition. Instead, it declares a function called a that returns an A and takes a pointer to a function taking nothing and returning an A.

    If you want an object definition, you have to add another pair of parenthesis:

    A a((A()));