3

I have created below singleton class and defined copy constructor and assignment operator as a private. When I invoke copy constructor or assignment operator, it does not call copy constructor and assignment operator(Maybe due to statically object creation). So my question is why singleton design pattern allows creating copy of object or assigning new object(which violates basic requirement of creating single instantiation of a class) form previously created object even they are declared private in a class?

Consider below code for details:-

#include <iostream>
#include "conio.h"
class singleton
{
static singleton *s;
          int i;

            singleton()
            {
            };          
            singleton(int x):i(x)
            { cout<<"\n Calling one argument constructor";
            };

            singleton(const singleton&)
            {
                cout<<"\n Private copy constructor";
            }

            singleton &operator=(singleton&)
            {
                cout<<"\n Private Assignment Operator";
            }

    public:
         static singleton *getInstance()
         {
            if(!s)
            {
                cout<<"\n New Object Created\n ";
                s=new singleton();
                return s;   
            }
             else
                {
                    cout<<"\n Using Old Object \n ";
                    return s;
                }

         }  
        int getValue()
        {
            cout<<"i = "<<i<<"\n";
            return i;
        }
        int setValue(int n)
        {
            i=n;
        }
};

singleton* singleton::s=0;

int main()
{
    // Creating first object

    singleton *s1=  singleton::getInstance();
    s1->getValue();

    singleton *s4=s1;    // Calling copy constructor-not invoking copy ctor
    singleton *s5;
    s5=s1;              // calling assignment operator-not invoking assign ope

    //Creating second object
    singleton *s2=singleton::getInstance();
    s2->setValue(32);
    s2->getValue();

    //Creating third object
    singleton *s3=singleton::getInstance();
    s3->setValue(42);
    s3->getValue();

    getch();
    return 0;
}

Am I missing something or my understanding is wrong.

Please help in this. Thanks in advance.

Venki WAR
  • 1,997
  • 4
  • 25
  • 38

1 Answers1

1

It is always the same object. You are using pointers to access that singleton here!

It is like having 3 egg boxes, but only one egg, that "over time" placed in the different boxes. That comparison isn't perfect, but hopefully close enough.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • You mean singleton allows multiple pointers point to same(single) object of a class?. But as per my understanding, singleton allow only single instantiation(single pointer) of a class. – HumbleSwagger May 12 '18 at 04:42
  • A pointer is not an instantiation! A pointer points to an instance. One egg, three boxes! But **one single** egg only! – GhostCat May 12 '18 at 04:44
  • Agree. In getInstance() I am creating only one object, to which multiple pointers are referring. I understood now that constructor needs to be private so that it will not allow object creation inside driver(main) code. But why we need assignment operator private then? – HumbleSwagger May 12 '18 at 05:09
  • We don't really need it. But usualy it is private because anyway we don't need them public. Also keep in mind that it is C++, so it is possible to write some silly code: `singleton * sgl_ptr = static_cast(malloc(sizeof(singleton ))); *sgl_ptr = sgl;`. – Konstantin T. May 12 '18 at 09:25