Advertisement

Singleton usage

Started by January 01, 2003 04:40 PM
5 comments, last by yckx 21 years, 10 months ago
Okay, I''ve been looking at singletons, and a basic one is set up like this:
  
class Singleton
{
     public:

          static Singleton* Instance();
};

Singleton* Singleton::Instance()
{
     static Singleton inst;
     return &inst
}
  
My problem is that I''m not sure how to access it. I can either assign it to a global Singleton* variable, which defeats much of the purpose; or I can declare a local Singleton* variable in every function that needs it, which seems messy. Is there a better way? Thanks yckx
I think you are missing the meaning of static here.

Static within the function will create a single instance of the class which will remain around for all time. The static instance() member of your class will enable you to access the single instance from anywhere in the program (Singleton::instance()). A different way of setting up the singleton is to use a static pointer and initialize it (ie allocate an instance) on the first call to instance().

I''m not sure exactly when the constructor for the static within the function will be called (anyone?), sometimes it is important, sometimes not.
Advertisement
JuNC: The constructor will be called the first time Instance() is called.

yckx: One of the main advantages of singletons is that you dont have an instance of the class anywhere. When you want to use a function that the singleton provides you call:


  Singleton::Instance()->MemberFunction();  


Ideally get yourself a copy of Design Patterns (Erich Gamma), or try reading this for more infomation about singletons.

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Okay, thanks guys. The articles I had read on singletons talked about the problems they addressed, and how to set up a class as a singleton, but kinda glossed over exactly how they were used in-code.

Thanks for the help!

yckx
Ah right, makes sense, in that case the pointer-member is redundant. Cheers.
Don't know if you're doing this already but it is good to make the constructor and the copy operator private, just to make sure that only one copy of the class is created.


        class Singleton{     public:              static Singleton* Instance();private:    // Prevent clients from creating a new Singleton    Singleton()	{};    // Prevent clients from creating a copy of the singleton    Singleton(const Singleton&);};  


I always find that putting a define in the header file makes accessing a little easier too...


        // Macro for easier access to sington instance objects#define g_Singleton	Singleton::Instance()  


Hope that helps,


=============================
All Animals should have afros
http://www.afrohorse.com
=============================


[edited by - Afrohorse on January 2, 2003 7:52:18 AM]
=============================All Animals should have afroshttp://www.afrohorse.com=============================
Advertisement
and the assignment operator.


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]

This topic is closed to new replies.

Advertisement