0

Please tell me why the singleton code below works? Every time When Singleton::instance() is called, an INSTANCE will be created, so are two INSTANCE supposed to be created in the following code?

#include <bits/stdc++.h>

using namespace std;

class Singleton
{
    private:
       Singleton() = default;

    public:
       static Singleton& instance()
       {
          static Singleton INSTANCE;
          return INSTANCE;
       }
};

int main()
{
    Singleton &s1 = Singleton::instance();
    Singleton &s2 = Singleton::instance();

    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
fizzbuzz
  • 159
  • 1
  • 1
  • 8
  • 1
    First of all, [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). And [`using namespace std;` is bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Some programmer dude Jul 27 '20 at 09:24
  • 4
    And I recommend you get [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read and learn more about what `static` does. – Some programmer dude Jul 27 '20 at 09:25
  • 3
    Not sure why you think there would be two instances created. Do you know what [static](https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables) does in this context? – Lukas-T Jul 27 '20 at 09:27
  • 1
    Do not learn something what [is considered a bad practice](https://stackoverflow.com/q/137975/1387438). – Marek R Jul 27 '20 at 09:30
  • I know what `static` means, but each time when expression `static Singleton INSTANCE;` executed. Isn't different `INSTANCE` created? – fizzbuzz Jul 27 '20 at 09:35
  • 1
    @fizzbuzz No, looks like you don't fully understand whtat `static` means :) _"... are initialized the first time control passes through their declaration [...] On all further calls, the declaration is skipped."_ – Lukas-T Jul 27 '20 at 09:36

3 Answers3

3

You should read about the static keyword. It can have different meaning and here you have two of them:

   static Singleton& instance()
   // ^ ---------------------------- (1)
   {
      static Singleton INSTANCE;
      // ^ ------------------------- (2)
      return INSTANCE;
   }

The first static declares the method instance as a "class-method" (or simply "static method"). You can call the method without having an instance.

The second static declares that INSTANCE has static storage duration. That is: There is only one. It is initialized once and when you reenter the function you get the exact same instance.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

you can go line by line:

1. 
Singleton &s1 = Singleton::instance();

you call here the static method instance(), inside that method you see the line

 static Singleton INSTANCE;

the trick here is to understand that variables declared static inside a method will only declared once, no matter how many times you call the method after. So it creates the instance of Singleton and it gets returned

 2.
Singleton &s2 = Singleton::instance();

the second call will actually skip the delaration of INSTANCE and will return that immediatly.

for more clarifying you can put some meaningful messages in the constructor

something like

std::cout << "Contructor called!" << std::endl;

even better if you use a static variable as a counter just for testing purposes.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Every time When Singleton::instance() is called, an INSTANCE will be created, so are two INSTANCE supposed to be created in the following code?

No. Only one instance is created.

each time when expression static Singleton INSTANCE; executed. Isn't different INSTANCE created?

No.

I know what static means

Evidently, you don't know what you think you know.

eerorika
  • 232,697
  • 12
  • 197
  • 326