Thread: Singleton Pattern Question

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Singleton Pattern Question

    Hi All

    I was going through Singleton design pattern and get to know that objects can be created only by static function of that class and constructors are make private.

    My question is, why assignment operators are not made private through which we can create a copy of already existing object.

    I tried below code and assignment works, so I have new object sc3.
    I know that its referring to memory of sc1 but finally I was able to create object without using static function.

    Also, why copy constructor not made as private.

    Below is code:

    Code:
    
    #include <iostream>
    
    using namespace std;
    
    class Singleton
    {
    private:
        static bool instanceFlag;
        static Singleton *single;
        Singleton()
        {
    		cout<<"\n Inside Constructor\n";
            //private constructor
        }
    public:
        static Singleton* getInstance();
        void method();
        ~Singleton()
        {
            instanceFlag = false;
        }
    };
    
    bool Singleton::instanceFlag = false;
    Singleton* Singleton::single = NULL;
    Singleton* Singleton::getInstance()
    {
        if(! instanceFlag)
        {
            single = new Singleton();
            instanceFlag = true;
            return single;
        }
        else
        {
            return single;
        }
    }
    
    void Singleton::method()
    {
        cout << "Method of the singleton class" << endl;
    }
    
    int main()
    {
        Singleton *sc1,*sc2,*sc3;
        sc1 = Singleton::getInstance();
    	sc3 = sc1;
    	sc1->method();
    	sc3->method();
        sc2 = Singleton::getInstance();
        //sc2->method();
        getchar();
        return 0;
    }

    Thanks

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    The way you've defined the Instance function is a bit off. You've got the allocation point wrapped in conditions. It's easier just to say:

    Code:
    Singleton::Singleton Instance()
    {
    
       if(!single){
          single = new Singleton();
       }
       return single;
    
    }
    As to the question. I think the entire point of a singleton is to allow universal access to a particular object (I've read some articles that say it is basically a fancy global).

    The copy constructor and assignment operator can be made private, I'm not really sure if there is a particular reason they shouldn't be?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Yes. That's I want to know that why copy constructor and assignment operator are spared from being private in standard Singleton definition.
    I look around on net and found same implementation every where.

    So AFAIK, singleton pattern don't allow other object to be created. And all objects are created via static method, which in turn called without object.

    There must be some specific reason for not privatizing assignment operator and copy constructor, but I didn;t anything relevant to it.

    If anybody has info on this, then please share.

    Thanks

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Your ability to search must be alarmingly poor.

    I found the top seven links from my search to contain examples with the private, unimplemented constructor and operator.

    In any event, the example code would still work with the private, unimplemented components.

    You are not copying the object. You are simply copying the pointer which points at the object.

    Also, you very likely don't have a good reason to use a "Singleton".

    If you only need one object, simply create only that one object.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regex Pattern Question
    By surefire in forum C Programming
    Replies: 3
    Last Post: 11-18-2009, 12:48 PM
  2. pattern checking in a string question..
    By transgalactic2 in forum C Programming
    Replies: 31
    Last Post: 12-26-2008, 04:54 PM
  3. Question about the Factory pattern.
    By h3ro in forum C++ Programming
    Replies: 14
    Last Post: 11-27-2008, 04:55 PM
  4. Binary Function - Design Pattern Question
    By Raven Arkadon in forum C++ Programming
    Replies: 9
    Last Post: 07-18-2007, 02:39 PM
  5. Singleton
    By Smallz in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2006, 11:57 PM