#include <iostream>
struct A
{
// constructor 1
A() { std::cout << "A() called" << std::endl; }
// constructor 2
A(const A&) { std::cout << "A(const A&) called" << std::endl; }
};
int main()
{
// statement 1
A x1;
// statement 2
A x2( A() );
return 0;
}
I've compiled it with GCC 4.7, 5.1 and 8.1. I got the same results from C++98 to C++17.
Statement 1 creates an A
variable that calls constructor 1.
Statement 2 declares a prototype of a function that takes a function pointer as its argument and returns a new A object:
A ( A (*)() )
I was expecting that statement 2 would create an A
variable that calls constructor 2 since I am initializing with a temporary A
object.
I'm not sure what I'm missing here. Can someone help me understand this unexpected situation?