Jump to content
  • Advertisement
Sign in to follow this  
monp

Understanding Singleton Pattern

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.h
template<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
}
};



Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 SINGLETON

template<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;

}

Share this post


Link to post
Share on other sites
Finally fixed it, Thanks for correcting the typo. After that i needed to define the constructor and destructor and it worked.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
i agree with argonaut...
Yeah sure singletons are great sometimes, but that doesn't mean we should use them everywhere.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!