Thread: Singleton Problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    Singleton Problem

    I want to make a generic singleton class which I can create a is-a relationship with.

    Looking around a bit I found something like this:

    Code:
    template <class T> Singleton{
    
        protected:
    
            Singleton();
            ~Singleton();
    
            static T* m_pInstance;
            ....
        public:
    
            static T* getInstance(){....}
    };
    Would something like this work if I had two different classes say Foo and Bar which inherit from Singleton.

    Singleton<Foo>::getInstance()->FooFunc();
    Singleton<Bar>::getInstance()->BarFunc();

    I think it should work since there would be two versions of m_pInstance... the one for class Foo and one for class Bar...

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Err.. it seems that you've mixed up meta programming and OOP pretty badly.
    Foo and Bar classes can be used as parameters to instantiate a class, but that doesn't mean they inherit from the generic class.
    When instantiating, you can have specializations for cases, but the generic code can also be used to do so automatically.

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

    That sort of thing is fine when done correctly.

    I can't magic away the fact that you didn't post the implementation, but I'd guess that what you've "found" is neither correct nor a singleton.

    If you want to know for sure what's going on and can't tell for yourself you'll have to get over it and post the implementation.

    That said, there is no reason for an object that is a singleton to have a "is-a" relationship with some singleton base class. That is an implementation detail and not relevant to a specific interface nor the nature of a singleton object.

    And that said, don't use singletons unless there is a spectacularly good reason. "I don't want to use a global.", "I only need once instance.", and "It is a strict resource." are all bad reasons for a class to be a singleton.

    *shrug*

    I don't know who said this first, but it is an excellent bit of advice: "Only create one instance if you only need one instance."

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singleton template problem
    By cloudy in forum C++ Programming
    Replies: 14
    Last Post: 01-11-2009, 05:40 AM
  2. Bug in Singleton class
    By marquito in forum C++ Programming
    Replies: 17
    Last Post: 12-14-2007, 01:02 PM
  3. Singleton
    By Smallz in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2006, 11:57 PM
  4. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 04:05 PM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 06:16 PM