How this code works

#include <iostream>
using namespace std;

class Test
{
public:
Test() { cout << "Hello from Test() "; }
} a;

int main()
{
cout << "Main Started ";
return 0;
}
the instance of Test, a, on line 8 (if you were using code-tags ;)) calls the Test ctor
How this line is printed before main() function because execution of statements will start after control to main function?
a is a global variable that exists in unnamed namespace and evaluated at compile, not run, time
The constructor of the class will be called when the object is created. A global variable is created before main() starts.

cout is another global variable that is created earlier, otherwise the output wouldn't work.

The order of creation of global variables is not specified unless it is in the same compiler unit (e.g. *.cpp).
Topic archived. No new replies allowed.