0

I'm trying to understand what's happening under the hood with the following piece of code. The question that I'm having trouble with is: How are static instances of classes handled in c++?

#include <iostream>

using namespace std;

class Shape {
   public:

      static Shape& getInstance() {
         static Shape obj;
         static Shape& objRef = obj;
         return objRef;
      }

      void init() {
          Shape singleton = Shape::getInstance();
          srand((unsigned)time(0));
          singleton.width = rand()%100;
          singleton.printWidth();
      }

      void printWidth(){
        cout<<width<<endl;
      }
      
   protected:
      int width = 0;
};

int main()
{
   Shape rectangle;
   rectangle.init();
   rectangle.printWidth();
   
   return 0;
}

The output of this code is:

37
0

But to me, this doesn't make sense. If a static instance of Shape is made anywhere within the program, shouldn't there only be one Shape in memory? If any shapes call printWidth after the singleton has set the width, shouldn't their width be equal to that width?

I would appreciate an explanation of where my thinking is going wrong.

Star-lordX
  • 39
  • 5
  • 2
    Your `shape` class is not a singleton. A singleton has private constructors so only the `get_instance` function can give you a handle to the single object. – NathanOliver Dec 09 '22 at 17:10
  • `getInstance` returns a copy, not a reference. The logic in it is also odd. You might read through [C++ Singleton design pattern](https://stackoverflow.com/questions/1008019/c-singleton-design-pattern) for some information about how to implement this correctly. – Retired Ninja Dec 09 '22 at 17:12
  • " shouldn't there only be one Shape in memory?" not sure why you think so. First line in main creates an instance, there is the static one in `getInstance` and you make a copy of that in `init` – 463035818_is_not_an_ai Dec 09 '22 at 17:12
  • `static Shape obj;` <--- this is the instance that exists once in memory no matter how often the function gets called. – 463035818_is_not_an_ai Dec 09 '22 at 17:14
  • More than one instance of a singleton is a contradiction in terms. – molbdnilo Dec 09 '22 at 17:14
  • 1
    I suggest you to write a constructor `Shape() { std::cout << "Hello WOrld"; }` and a destructor `~Shape() { std::cout << "Bye!"; }` to get a better idea of how many instances there are created – 463035818_is_not_an_ai Dec 09 '22 at 17:16

1 Answers1

1

Singleton, by definition, means that you'll have only one instance.

However, static does not ensure it's singleton. Your code's getInstance() would like to be a common instance getter pattern: you have access to a common instance, but you can also create (arbitrary many) instances as required. So your local instance, rectangle, won't be the one that init() initializes. What's even worse, in your code, you returns a copy of the common instance, not a reference to it (e.g. static Shape& getInstance() would solve this), so you have 3 instances we're talking about: one in getInstance(), one in init() that's printed and then dropped; and one in main. If you fix it with reference return, you'll still need to use references (initialized by getInstance()) to make it work.

lorro
  • 10,687
  • 23
  • 36