Thread: Singleton Classes

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Singleton Classes

    A tutorial I came across more recently reads:

    One problem with public constructors is that they do not provide any way to control how many of a particular class may be created. If a public constructor exists, it can be used to instantiate as many class objects as the user desires. Often it is useful to restrict users to being able to create only one instance of a particular class. Classes that can only be instantiated once are called singletons. There are many ways to implement singletons, but most of them involve use of a private (or protected) constructor to prevent users from instantiating as many of the class as they want
    The author stopped there and did not speak on it any further. I'd like to know more about this subject.

    How can I do this? Please, use as much detail as you can, and thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You make public function getInstance

    and static member _instance

    getInstance will check if _instance is nullPrt - it will call private constructor, assign result to _instance and return it - otherwise just returns _instance

    It can also implement refCounter - to check when the _instance could be freed... But with Singleton the object could be left to live till the end of program even it is not used...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Quote Originally Posted by vart View Post
    You make public function getInstance

    and static member _instance

    getInstance will check if _instance is nullPrt - it will call private constructor, assign result to _instance and return it - otherwise just returns _instance

    It can also implement refCounter - to check when the _instance could be freed... But with Singleton the object could be left to live till the end of program even it is not used...
    So you're suggesting I just use a variable that indicates whether an instance exists or not? An example of what I think you mean, below:

    Code:
    class instance {
        bool instanced;
    
    
        void initiate() {
            // do initiation programming...
        }
    
    
        public:
    
    
            instance() {
                if(!instanced) {
                    instanced = true;
                    initiate();
                }
            }
    
    
            ~instance() {
                instanced = false;
            }
    };

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    You probably want
    Code:
    static bool instanced;
    Instead of
    Code:
    bool instanced;

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Quote Originally Posted by SirPrattlepod View Post
    You probably want
    Code:
    static bool instanced;
    Instead of
    Code:
    bool instanced;
    I've tried that and it doesn't work; I get an error, as seen below:

    1>Third.obj : error LNK2001: unresolved external symbol "private: static bool instance::instanced" (?instanced@instance@@0_NA)

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    In the .cpp file you have to "give it memory":

    bool instance::instanced;

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    with such approach you can not prevent creating new instance

    What i mean is

    Code:
    class st
    {
       public:
         static st* getInstance()
         {
            if(_st != nullptr) return _st;
            _st = new st();
            return _st;
         }
    private:
        static st* _st;
        st() {};
        st(const st&);
        st& operator= (const st&);
    };
    st* st::_st = nullptr;
    
    int main()
    {
        {
            //first function
            st* singletonvar = st::getInstance();
        }
    
        {
            //second function
            st* singletonvar2 = st::getInstance();
        }
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singleton Problem
    By Homunculus in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2012, 06:02 PM
  2. Should I make this a singleton?
    By g4j31a5 in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 09:13 PM
  3. Singleton
    By Smallz in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2006, 11:57 PM
  4. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM
  5. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 04:05 PM