2

I am asking because I am curious while studying the singletone pattern.

I want to know the difference between singletone pattern and static variable.

class A
{
public:
    A() {}
    ~A() {}
};

static A* a;

This

class Singleton
{
private:
    Singleton() {}
    ~Singleton() {}
private:
    static Singleton* m_Singleton;
    
public:
    static Singleton* GetInstance()
    {
        if (m_Singleton == nullptr)
        {
            m_Singleton = new Singleton();
        }
        return m_Singleton;
    }
    static void DestroyInstance()
    {
        if (m_Singleton == nullptr)
        {
            return;
        }
        delete m_Singleton;
        m_Singleton = nullptr;
    }
};

Singleton* Singleton::m_Singleton = nullptr;

I want to know the difference between this. Aren't you creating an object using only one? Or I wonder if there's any difference.

Brave
  • 21
  • 3
  • you can create more than one instance of `A` but there can be only one `Singleton` – 463035818_is_not_an_ai Aug 04 '22 at 12:49
  • With a singleton there is only ever one `Foo`. With your first exmaple there can be `N` `Foo`s – NathanOliver Aug 04 '22 at 12:49
  • In what case can you make two? No matter how hard I think about it, I don't know. – Brave Aug 04 '22 at 12:51
  • `A a,b;` is two instances of type `A` – 463035818_is_not_an_ai Aug 04 '22 at 12:51
  • Note that `static A* a;` does not actually declare a variable of type `A`, just an (uninitialized) pointer to one. – DevSolar Aug 04 '22 at 12:56
  • 1
    `static` in namespace scope means something else then `static` in class/struct/function scope! `static` in namespace scope means this symbol is visible only in current translation unit. So when you put `static A* a;` in header you will have multiple instances of `a` one for each translation unit including that header. [see dupe](https://stackoverflow.com/q/6034654/1387438). – Marek R Aug 04 '22 at 13:14
  • An important difference is, beside instance count, the static destruction order. This might affect whether your program crashes or not during exit. – lorro Aug 04 '22 at 13:30

2 Answers2

0

The difference is that the user (the programmer that use your class) can create as many instances of A as he/she wants. The porpouse of the singleton design pattern is to be sure to have just one instance of singleton class (Singleton in your case) in the whole program. This is possible declaring the constructor private and then delegate to a static function the duty to return an instance of the class.

Marco Beninca
  • 605
  • 4
  • 15
-3

If you refere to a sinlgleton in combination with static you potentially want a setup the static version because it is thread save since C++ 11.

class A
{
public:
    A() {}
    ~A() {}
};

A* getInstanceOfA() 
{
    static A a; // Thread save magic static implementing singleton
    return &a;
}

A* globalSingleton = nullptr;

A* getInstanceOfAWithNew() 
{
    if(nullptr == globalSingleton) {
        globalSingleton = new A(); // Not thread save. Would need mutex.
    }
    return globalSingleton;
}

The C++ 11 standard guarantees these kind of static variables to be thread safe. The compiler will make sure the static variable a is initialized only once. This isn't given if you use new A. You need to use some guard.

See How to implement multithread safe singleton in C++11 without using

Totonga
  • 4,236
  • 2
  • 25
  • 31