Tuesday, November 30, 2010

Instantiation Order of Global Objects

The following program my crash:

#include <iostream>
using namespace std;

class MyClass
{
 public:
   MyClass()
   {
      cout << "Entering constructor\n";
   }
};

static MyClass myObject;

int main()
{
   return 0;
}


On SunOs 5.9, it crashes because the order of the instantiation of the global objects are not guaranteed. In this case the cout object is not created before the myObject object. If you add <stdio.h> and call printf() instead of cout, the program does not crash.

No comments:

Post a Comment