Understanding Singleton Pattern

Started by
25 comments, last by valentin-galea 15 years, 11 months ago
Could someone explain to me, how to use a singleton pattern? I tried following this tutorial but I tried to make it on my own and I am getting errors. main.cpp: undefined refrence to Singleton<Data>::Instance(); main.cpp: undefined refrence to Singleton<Data>::ptr_getSingleton()->PrintData(5); What would I even Define for these to work?

//Singleton.h
template<class T>
class Singleton
{
	private:
		static T *m_instance;
	protected:
		Singleton();
	public:
		static T *Instance();
		static T &ref_getSingleton();
		static T *ptr_getSingleton();
};


//Data.h
class Data
{
	private:
	int iData;
	public:
	int PrintData(int iData)
	{
		std::cout << iData << std::endl;
	}
};


#include <iostream>
#include "Singleton.h"
#include "Data.h"

int main()
{
    Singleton<Data>::Instance();
	Singleton<Data>::ptr_getSingleton()->PrintData(5);
    return 0;

}

Advertisement
Okay, not to be rude but since it seems that you're pretty new to programming I'd recommend the best course of action is to forget you ever learned about the Singleton pattern. It normally leads to horrid code and in 99.9% of cases is actually the wrong choice. If you do a search for singleton on these forums a lot of very smart people have already explained why its a bad idea.

In most cases if you need global access to something, make it a global, like a logger. The only time I can see justifying a Singleton is when you have a hardware limitation that only allows one instance. Otherwise if you only want one Resource Pool (what most would call a manager) then just create one. Enforcing limitations on programmers by using a Singleton is the wrong way to do things.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

The compilation fails because the compiler cannot find the implementation of Singleton<Data>::Instance() (the member function Instance() of a Singleton template compiled with Data as the template parameter). You can get it to compile by defining the member function in the header file.

//Singleton.htemplate<class T>class Singleton{	private:		static T *m_instance;	protected:		Singleton()		{			// implement this here		}	public:		static T *Instance()		{			// implement this here		}		static T &ref_getSingleton()		{			// implement this here		}		static T *ptr_getSingleton()		{			// implement this here		}};
I'd suggest that you search the forum archives using the keyword 'singleton' and read three or four threads on the topic.

Meanwhile, out of curiosity, how are you planning to use your singleton class? What problem is it exactly that you're trying to solve?
I was going to rebuild a 2d engine to find out how it works and it uses the singleton pattern in it. So I decided to look it up also.
http://gpwiki.org/index.php/SDL:Tutorials:Complete_2D_Engine_Overview
http://wiki.gamedev.net/index.php/Singleton_pattern



[Edited by - monp on May 13, 2008 9:46:30 PM]
I am using the same data.h and used a different singleton.h and I am down to one error now. GetSingletonPtr not a member of Singleton<Data>

//singleton.h#ifndef SINGLETON#define SINGLETONtemplate<class T>class Singleton{	private:		Singleton();		~Singleton();		Singleton(Singleton const&);		Singleton& operator=(Singleton const&);	public:		static T* GetSingletonPtr();		{			static T m_Instance;			return &m_Instance;		}};#endif


#include <iostream>#include "Singleton.h"#include "Data.h"int main(){	Singleton<Data>::GetSinlgetonPtr()->PrintData(5);    return 0;}
GetSinlgetonPtr()


Typo perhaps?

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Finally fixed it, Thanks for correcting the typo. After that i needed to define the constructor and destructor and it worked.
Quote:Original post by Mike2343
Okay, not to be rude but since it seems that you're pretty new to programming I'd recommend the best course of action is to forget you ever learned about the Singleton pattern. It normally leads to horrid code and in 99.9% of cases is actually the wrong choice. If you do a search for singleton on these forums a lot of very smart people have already explained why its a bad idea.

In most cases if you need global access to something, make it a global, like a logger. The only time I can see justifying a Singleton is when you have a hardware limitation that only allows one instance. Otherwise if you only want one Resource Pool (what most would call a manager) then just create one. Enforcing limitations on programmers by using a Singleton is the wrong way to do things.


Not to be rude, but not everyone learns programming on the same course. Singletons are one of the design patterns because they are a design pattern, and they are still taught quite heavily in courses on design patterns. I'm not ninja by any means, but it was rather late into my course studies that I learned design patterns. Since then, I've found them useful once in awhile.
~Argonaut________________________________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.
i agree with argonaut...
Yeah sure singletons are great sometimes, but that doesn't mean we should use them everywhere.

This topic is closed to new replies.

Advertisement