user3629119 user3629119 - 3 months ago 25
C++ Question

why in singleton pattern , we do make copy constructor and assignment operator as private?

In a singleton pattern, typically we make the constructor/destructor private. That I understand because we don't want the user to create/delete the singleton object. There should be only way to get or create the instance. However, I don't understand why do we need to make the copy constructor and assignment operator as private. What is the advantage of making the copy constructor and assignment operator private in singleton.

Answer
Singleton obj1 = Singleton::CreateInstacnce();
    Singleton obj2 = obj1; // copy ctr gets called
    Singleton obj3;
    obj3 = obj1;  // assignment operator gets called

Therefore, if you don't make them private, multiple instance of Singleton class will be created