C++
Objects of this class cout
s messages when they are constructed and destroyed. I tried to construct a temporary object using a declaration of only the class name, but it gave an unexpected output.
In #1, I instantiate a temporary nameless object using parentheses.
In #2, I instantiate a temporary nameless object using uniform initialization.
I didn't know whether #3 would compile. I only thought that if #3 were to compile, it would signify a construction of a temporary nameless object. It does compile, but no object is constructed as seen from the blankness of the console output under #3. What is happening here?
#include <iostream>
class A
{
public:
A() {std::cout << "constructed\n";}
~A() {std::cout << "destroyed\n";}
};
auto main() -> int
{
std::cout << "#1:\n";
A();
std::cout << "#2:\n";
A{};
std::cout << "#3:\n";
A;
return 0;
}
Console Output:
#1:
constructed
destroyed
#2:
constructed
destroyed
#3:
Note: This was compiled in VC11 with November 2012 CTP. It doesn't compile in g++ 4.8.0 or clang 3.2, which gives error: declaration does not declare anything [-fpermissive]
and fatal error: 'iostream' file not found
, respectively.