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. Singletone Design Pattern

Singletone Design Pattern

Scheduled Pinned Locked Moved C / C++ / MFC
questiondesignregexarchitecture
7 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.
  • K Offline
    K Offline
    knoxplusplus
    wrote on last edited by
    #1

    Hi !!! Can me somebody tell what is an singletone design pattern? Please give me some little code samle. Thanks.:cool: -:KNOX:-

    C G 2 Replies Last reply
    0
    • K knoxplusplus

      Hi !!! Can me somebody tell what is an singletone design pattern? Please give me some little code samle. Thanks.:cool: -:KNOX:-

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

      Singleton on wikipedia[^]


      Cédric Moonen Software developer
      Charting control

      1 Reply Last reply
      0
      • K knoxplusplus

        Hi !!! Can me somebody tell what is an singletone design pattern? Please give me some little code samle. Thanks.:cool: -:KNOX:-

        G Offline
        G Offline
        G Haranadh
        wrote on last edited by
        #3

        search with this key word in code project you will get many. :) One example from CP: Singleton Pattern & its implementation with C++ --------------------------------------------------------------------

        class my_singleton
        {
        private:
        std::string my_string;
        my_singleton() : my_string("foo bar bash") {}

        public:
        static my_singleton &instance()
        {
        static my_singleton global_instance;
        return global_instance;
        }

        std::string get_string() { return my_string; }
        };
        ...
        cout << my_singleton::get_instance().get_string() << endl;

        Nice talking to you. :-O
        If you judge people, you have no time to love them. -- Mother Teresa

        B K 2 Replies Last reply
        0
        • G G Haranadh

          search with this key word in code project you will get many. :) One example from CP: Singleton Pattern & its implementation with C++ --------------------------------------------------------------------

          class my_singleton
          {
          private:
          std::string my_string;
          my_singleton() : my_string("foo bar bash") {}

          public:
          static my_singleton &instance()
          {
          static my_singleton global_instance;
          return global_instance;
          }

          std::string get_string() { return my_string; }
          };
          ...
          cout << my_singleton::get_instance().get_string() << endl;

          Nice talking to you. :-O
          If you judge people, you have no time to love them. -- Mother Teresa

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #4

          Hi, this class isn't really a singleton, because you can make more than one instances of it. You need to change the modifier of the constructor from public to at least protected. This way one is forced to use the instance function.

          class my_singleton
          {
          private:
           std::string my_string;
           my_singleton() : my_string("foo bar bash") {}
          
          public:
           static my_singleton &instance()
           {
            static my_singleton global_instance;
            return global_instance;
           }
           
           std::string get_string() { return my_string; }
          };
          ...
          cout << my_singleton::instance().get_string() << endl;
          

          codito ergo sum

          G 1 Reply Last reply
          0
          • B BadKarma

            Hi, this class isn't really a singleton, because you can make more than one instances of it. You need to change the modifier of the constructor from public to at least protected. This way one is forced to use the instance function.

            class my_singleton
            {
            private:
             std::string my_string;
             my_singleton() : my_string("foo bar bash") {}
            
            public:
             static my_singleton &instance()
             {
              static my_singleton global_instance;
              return global_instance;
             }
             
             std::string get_string() { return my_string; }
            };
            ...
            cout << my_singleton::instance().get_string() << endl;
            

            codito ergo sum

            G Offline
            G Offline
            G Haranadh
            wrote on last edited by
            #5

            Oh. Sorry & Thanks for correcting me.:rose: Nice talking to you. :-O
            If you judge people, you have no time to love them. -- Mother Teresa

            1 Reply Last reply
            0
            • G G Haranadh

              search with this key word in code project you will get many. :) One example from CP: Singleton Pattern & its implementation with C++ --------------------------------------------------------------------

              class my_singleton
              {
              private:
              std::string my_string;
              my_singleton() : my_string("foo bar bash") {}

              public:
              static my_singleton &instance()
              {
              static my_singleton global_instance;
              return global_instance;
              }

              std::string get_string() { return my_string; }
              };
              ...
              cout << my_singleton::get_instance().get_string() << endl;

              Nice talking to you. :-O
              If you judge people, you have no time to love them. -- Mother Teresa

              K Offline
              K Offline
              knoxplusplus
              wrote on last edited by
              #6

              Thanks for the help. :cool: -:KNOX:-

              G 1 Reply Last reply
              0
              • K knoxplusplus

                Thanks for the help. :cool: -:KNOX:-

                G Offline
                G Offline
                G Haranadh
                wrote on last edited by
                #7

                You are always welcome :rose: Nice talking to you. :-O
                If you judge people, you have no time to love them. -- Mother Teresa

                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