Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 689
Kundan Kumar Avatar asked Nov 30 '22 23:11

Kundan Kumar


1 Answers

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()));
like image 90
fredoverflow Avatar answered Jan 05 '23 00:01

fredoverflow