I saw this question about constructors and I don't understand why the variable a
is calling the constructor.
I thought it wass an error because the variable declaration is out of main
without stating global before the name of it and they only wrote a;
without the class name before it. How does the compiler know that the variable is of type Test
?
#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Hello from Test() "; }
} a;
int main()
{
cout << "Main Started ";
return 0;
}
The answer for the output was - "Hello from Test() Main Started".