Like Tree1Likes
  • 1 Post By phantomotap

Singleton Problem

This is a discussion on Singleton Problem within the C++ Programming forums, part of the General Programming Boards category; I want to make a generic singleton class which I can create a is-a relationship with. Looking around a bit ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,281
    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
    laserlight likes this.

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, 04:40 AM
  2. Bug in Singleton class
    By marquito in forum C++ Programming
    Replies: 17
    Last Post: 12-14-2007, 12: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, 03:05 PM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21