Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Singleton Class (C++)

Singleton Class (C++)

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Scorpio
    wrote on last edited by
    #1

    how do i make a class as singleton class.

    C M 2 Replies Last reply
    0
    • S Scorpio

      how do i make a class as singleton class.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      class CSingleton
      {
      public:
      static CSingleton* GetInstance();

      private:
      void CSingleton();
      CSingleton ~CSingleton();

      static CSingleton* m_pInstance;
      };

      In the cpp file:

      CSingleton* CSingleton::m_pInstance = NULL;

      // Constructor + Destructor
      .....

      CSingleton* CSingleton::GetInstance()
      {
      if (!m_pInstance)
      m_pInstance = new CSingleton;
      return m_pInstance;
      }


      Cédric Moonen Software developer
      Charting control

      S 1 Reply Last reply
      0
      • S Scorpio

        how do i make a class as singleton class.

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3

        Singleton C++ sample in Wiki[^].


        Maxwell Chen

        S 1 Reply Last reply
        0
        • M Maxwell Chen

          Singleton C++ sample in Wiki[^].


          Maxwell Chen

          S Offline
          S Offline
          Scorpio
          wrote on last edited by
          #4

          Thank You very much:-D

          1 Reply Last reply
          0
          • C Cedric Moonen

            class CSingleton
            {
            public:
            static CSingleton* GetInstance();

            private:
            void CSingleton();
            CSingleton ~CSingleton();

            static CSingleton* m_pInstance;
            };

            In the cpp file:

            CSingleton* CSingleton::m_pInstance = NULL;

            // Constructor + Destructor
            .....

            CSingleton* CSingleton::GetInstance()
            {
            if (!m_pInstance)
            m_pInstance = new CSingleton;
            return m_pInstance;
            }


            Cédric Moonen Software developer
            Charting control

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            A simpler way: -------------- // Console.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; class Single { public: static Single& GetInstance() { static Single s; return s; } void Method() const { cout << "Method called : this = 0x" << hex << this << endl; } private: Single() { cout << "Instance created!" << endl; }; }; int main(int argc, char* argv[]) { Single::GetInstance().Method(); Single::GetInstance().Method(); Single::GetInstance().Method(); return 0; } -------------- This technique leverages the langauges rules: A static member is created the first time it's encountered. This way we don't need the "new" or the "if" or to remember to "delete" it. Steve

            C 1 Reply Last reply
            0
            • S Stephen Hewitt

              A simpler way: -------------- // Console.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; class Single { public: static Single& GetInstance() { static Single s; return s; } void Method() const { cout << "Method called : this = 0x" << hex << this << endl; } private: Single() { cout << "Instance created!" << endl; }; }; int main(int argc, char* argv[]) { Single::GetInstance().Method(); Single::GetInstance().Method(); Single::GetInstance().Method(); return 0; } -------------- This technique leverages the langauges rules: A static member is created the first time it's encountered. This way we don't need the "new" or the "if" or to remember to "delete" it. Steve

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Yes, that another way of doing it. Both techniques have their pro and cons. I remember having read an article that was discussing several singleton implementations but I don't remember the link. Yours is great when it has to be used 'alone' because as you said you don't have to worry about releasing the instance. But in some cases, this can lead to problems: if you have a singleton that is member of another singleton, then you get into trouble because you have no control over the timings of the destruction of the instances. In such case, you need to control yourself the scope of the instances.


              Cédric Moonen Software developer
              Charting control

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups