This is a very basic question about C++. Why isn't a constructor invoked for the statement "A x(A())"?
In the code that follows - which I have run with g++ 4.8.2 - the constructor (as well as the destructor) is called only once. This may be due to optimization but I am curious about the type of x
- reported by typeid - which is "A(A (*)())".
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
class A{
public:
~A() { cout << "Destructed" << endl; }
};
int main() {
int status;
A x(A());
cout << abi::__cxa_demangle(typeid(x).name(),0,0,&status) << endl;
A a ;
cout << abi::__cxa_demangle(typeid(a).name(),0,0,&status) << endl;
return 0;
}