I do not know the difference between global and static objects execution so far. They differ a little in run time.




[Code]

class Object

{

public:

  char* name;

  Object(char* name) { printf("Object::Object(%s)\n", name); this->name = name; }

  virtual ~Object()  { printf("Object::~Object(%s)\n", name); }

};

 

Object g_object("global"); // When is this intialized?

 

void foo()

{

  static Object s_object("static"); // When is this initialized?

}

 

int main()

{

  printf("main begin\n");

 

  foo();

 

  printf("main end\n");

}




[Result]

Object::Object(global) // created before entering main

main begin

Object::Object(static) // created when it turns into an accessible state.

main end

Object::~Object(static)

Object::~Object(global)




[Conclusion]

Global objects are initialized before execution is entered into main function, while static objects are initialized when the objects turn into an accessible state.




[Download]

global_static_test.zip