View Single Post
Go4Expert Member
5Feb2009,20:18  
zamjad's Avatar
Quote:
Originally Posted by jayaraj_ev View Post
Hi,
Good that u tried to do one without dynamic allocation. But one thing we should be carefull about is Static objects get created at start of program, so the objects gets created before main() which calls its constructor before. How we implement inside constructor also matters.
That happens with only global static object. Here object will create only when you call this function first time.

In fact you can always make a program to test it. Here is the simple test program to verify this

Code:
#include <iostream>

using std::cout;
using std::endl;

class Singleton
{
private:
	Singleton()
	{
		cout << "Singleton::Singleton" << endl;
	}

	~Singleton() { }

public:
	static Singleton* CreateObject()
	{
		static Singleton obj;
		return &obj;
	}
};

int main()
{
	return 0;
}