Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can any one provide me a sample of Singleton in c++?

I write a singleton c++ in the follow way:

class A {     private:         static A* m_pA;         A();         virtual ~A();      public:         static A* GetInstance();         static void FreeInstance();          void WORK1();         void WORK2();         void WORK3();     } }  A* A::GetInstance() {     if (m_pA == NULL)         m_pA = new A();     return m_pA; }  A::~A() {     FreeInstance()  // Can I write this? are there any potential error? }  void A::FreeInstance() {     delete m_pA;     m_pA = NULL; } 

Thanks! Evan Teran and sep61.myopenid.com 's answer is right, and really good! My way is wrong, I wish any one writting such code can avoid my silly mistake.

My singleton A in my project has a vector of smart pointer, and another thread can also edit this vector, so when the application is closing, it always become unstable even I add lots of CMutex. Multithread error + singleton error wasted me 1 day.

//----------------------------------------------------------- A new singleton, you are welcome to edit if you think there is any problem in the following sample:

class A {     private:         static A* m_pA;         explicit A();         void A(const A& a);         void A(A &a);         const A& operator=(const A& a);         virtual ~A();      public:         static A* GetInstance();         static void FreeInstance();          void WORK1();         void WORK2();         void WORK3();     } }  A* A::GetInstance() {     if (m_pA == NULL){         static A self;         m_pA = &self;     }     return m_pA; }  A::~A() { } 
like image 422
user25749 Avatar asked Nov 07 '08 01:11

user25749


People also ask

What is singleton in C?

Singleton in C++ Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables. Although they're super-handy, they break the modularity of your code.

How do you make a class singleton in C++?

Solution. Create a static member that is a pointer to the current class, restrict the use of constructors to create the class by making them private , and provide a public static member function that clients can use to access the single, static instance.

When should I use a singleton C++?

The only time you should use a singleton is in the rare case where it is VITAL that there only ever be one of these objects. This simply doesn't come up nearly as often as you might think.

What is the pattern that limits the number of instances of a class normally to 1?

In object-oriented programming , Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the JVM (Java Virtual Machine). In other words, a class should ensure that only a single instance must be created and single object can be used by all other classes.


2 Answers

Why does everybody want to return a singleton as a pointer?
Return it as a reference seems much more logical!

You should never be able to free a singleton manually. How do you know who is keeping a reference to the singleton? If you don't know (or can't guarantee) nobody has a reference (in your case via a pointer) then you have no business freeing the object.

Use the static in a function method.
This guarantees that it is created and destroyed only once. It also gives you lazy initialization for free.

class S {     public:         static S& getInstance()         {             static S    instance;             return instance;         }     private:         S() {}         S(S const&);              // Don't Implement.         void operator=(S const&); // Don't implement  }; 

Note you also need to make the constructor private. Also make sure that you override the default copy constructor and assignment operator so that you can not make a copy of the singleton (otherwise it would not be a singleton).

Also read:

  • https://stackoverflow.com/a/1008289/14065
  • Singleton: How should it be used
  • C++ Singleton design pattern

To make sure you are using a singleton for the correct reasons.

Though technically not thread safe in the general case see:
What is the lifetime of a static variable in a C++ function?

GCC has an explicit patch to compensate for this:
http://gcc.gnu.org/ml/gcc-patches/2004-09/msg00265.html

like image 158
Martin York Avatar answered Oct 01 '22 01:10

Martin York


You can avoid needing to delete it by using a static object like this:

if(m_pA == 0) {     static A static_instance;     m_pA = &static_instance; } 
like image 31
Evan Teran Avatar answered Sep 30 '22 23:09

Evan Teran