Templated singleton classes

This is a discussion on Templated singleton classes within the C++ Programming forums, part of the General Programming Boards category; I'm using the following singleton model for all of my resource managers. Code: #pragma once class CSingleton { private: static ...

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586

    Templated singleton classes

    I'm using the following singleton model for all of my resource managers.


    Code:
    #pragma once
    
    class CSingleton
    {
      private:
        static CSingleton *m_pInstance;
      protected:
        CSingleton();
        CSingleton(const CSingleton&);
        CSingleton& operator(const CSingleton&);
      public:
        static CSingleton *Instance();
    };
    Code:
    #include "CSingleton.h"
    
    CSingleton *CSingleton::m_pInstance=NULL;
    
    CSingleton *CSingleton::Instance()
    {
      if (m_pInstance==NULL)  m_pInstance=new CSingleton;
      return m_pInstance;
    }
    My resource managers are then derived from CSingleton.
    How would I go about creating a templated version of this singleton class to allow for differing types w/o having to derive from CSingleton?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    You could try something like this

    Code:
    #pragma once
    
    template<class T>
    class CSingleton
    {
      private:
        static T *m_pInstance;
      protected:
        CSingleton();
        CSingleton(const CSingleton&);
        CSingleton& operator=(const CSingleton&);
      public:
        static T *Instance();
    };
    Code:
    #include "CSingleton.h"
    
    T *CSingleton::m_pInstance=0;
    
    T *CSingleton::Instance()
    {
      if (m_pInstance == 0)  m_pInstance=new T();
      return m_pInstance;
    }

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The Loki library contains a singleton template. The book Modern C++ Design describes it.
    http://loki-lib.sourceforge.net/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  2. Templated classes...
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2006, 04:31 PM
  3. templated classes
    By homeyg in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2006, 08:07 AM
  4. Cannot declare STL iterators in custom templated classes!
    By Maelstrom in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2006, 06:58 PM
  5. Pointers to (templated) classes being declared
    By reanimated in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2006, 08:30 AM

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