Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. When to use static instance of class in c++ ?
Forum Updated to NodeBB v4.3 + New Features

When to use static instance of class in c++ ?

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 691 Views
  • 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    I am reading one project code there i have seen that in several places they use the

    like in thread related class , database access related class.

    I am Not knowing the why they use the static instance of class.

    
    #ifndef SRC_OTATHREAD_H_
    #define SRC_OTATHREAD_H_
    
    #include <queue>
    #include <vector>
    #include <pthread.h>
    #include <evt/libevt.h>
    #include <boomer.h>
    
    namespace app
    {
    
    namespace otathread {
    
    enum OtaThreadState {START=0, STOP=1, CONTINUE=3} ;
    
    class OtaThread
    {
    public:
        static OtaThread *getInstance() ;
    
        void initImpl();
    
        int initAndStartThread() ;
        void stopThread();
        void checkForOTAThread();
        OtaThreadState runImpl(OtaThreadState) ;
        pthread_t _thread;
        int getBackSyncTimeout();
        static boomer::transaction::TransactionResult SyncTimeOutCallback(void *NotUsed, int argc, char**argv, char** azColName);
    private:
        OtaThread();
        ~OtaThread();
        static void *runThreadFunc(void *This) ;
        void run(void) ;
    
        Event mEvent;
        int OtaTimer,defaultTimer;
        int deviceTimer;
        static OtaThread *otaThreadInstance;
        static bool otaThreadActive;
        static int NumberofOTAcall;
    
    };
    
    } /*namespace otathread*/
    
    } /*namespace app*/
    #endif /* SRC_OTATHREAD_H_ */
    
    JonBJ KroMignonK 2 Replies Last reply
    0
    • Q Qt embedded developer

      I am reading one project code there i have seen that in several places they use the

      like in thread related class , database access related class.

      I am Not knowing the why they use the static instance of class.

      
      #ifndef SRC_OTATHREAD_H_
      #define SRC_OTATHREAD_H_
      
      #include <queue>
      #include <vector>
      #include <pthread.h>
      #include <evt/libevt.h>
      #include <boomer.h>
      
      namespace app
      {
      
      namespace otathread {
      
      enum OtaThreadState {START=0, STOP=1, CONTINUE=3} ;
      
      class OtaThread
      {
      public:
          static OtaThread *getInstance() ;
      
          void initImpl();
      
          int initAndStartThread() ;
          void stopThread();
          void checkForOTAThread();
          OtaThreadState runImpl(OtaThreadState) ;
          pthread_t _thread;
          int getBackSyncTimeout();
          static boomer::transaction::TransactionResult SyncTimeOutCallback(void *NotUsed, int argc, char**argv, char** azColName);
      private:
          OtaThread();
          ~OtaThread();
          static void *runThreadFunc(void *This) ;
          void run(void) ;
      
          Event mEvent;
          int OtaTimer,defaultTimer;
          int deviceTimer;
          static OtaThread *otaThreadInstance;
          static bool otaThreadActive;
          static int NumberofOTAcall;
      
      };
      
      } /*namespace otathread*/
      
      } /*namespace app*/
      #endif /* SRC_OTATHREAD_H_ */
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #3

      @Qt-embedded-developer said in When to use static instance of class in c++ ?:

      I am Not knowing the why they use the static instance of class.

      This implementation uses static to implement the class a singleton class.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      Q 1 Reply Last reply
      4
      • Q Qt embedded developer

        I am reading one project code there i have seen that in several places they use the

        like in thread related class , database access related class.

        I am Not knowing the why they use the static instance of class.

        
        #ifndef SRC_OTATHREAD_H_
        #define SRC_OTATHREAD_H_
        
        #include <queue>
        #include <vector>
        #include <pthread.h>
        #include <evt/libevt.h>
        #include <boomer.h>
        
        namespace app
        {
        
        namespace otathread {
        
        enum OtaThreadState {START=0, STOP=1, CONTINUE=3} ;
        
        class OtaThread
        {
        public:
            static OtaThread *getInstance() ;
        
            void initImpl();
        
            int initAndStartThread() ;
            void stopThread();
            void checkForOTAThread();
            OtaThreadState runImpl(OtaThreadState) ;
            pthread_t _thread;
            int getBackSyncTimeout();
            static boomer::transaction::TransactionResult SyncTimeOutCallback(void *NotUsed, int argc, char**argv, char** azColName);
        private:
            OtaThread();
            ~OtaThread();
            static void *runThreadFunc(void *This) ;
            void run(void) ;
        
            Event mEvent;
            int OtaTimer,defaultTimer;
            int deviceTimer;
            static OtaThread *otaThreadInstance;
            static bool otaThreadActive;
            static int NumberofOTAcall;
        
        };
        
        } /*namespace otathread*/
        
        } /*namespace app*/
        #endif /* SRC_OTATHREAD_H_ */
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Qt-embedded-developer said in When to use static instance of class in c++ ?:

        why they use the static instance of class.

        What "static instance of a class" are you talking about?

        This class has some static member variables. All instances of the class will share those same static variables.

        1 Reply Last reply
        0
        • Q Qt embedded developer

          I am reading one project code there i have seen that in several places they use the

          like in thread related class , database access related class.

          I am Not knowing the why they use the static instance of class.

          
          #ifndef SRC_OTATHREAD_H_
          #define SRC_OTATHREAD_H_
          
          #include <queue>
          #include <vector>
          #include <pthread.h>
          #include <evt/libevt.h>
          #include <boomer.h>
          
          namespace app
          {
          
          namespace otathread {
          
          enum OtaThreadState {START=0, STOP=1, CONTINUE=3} ;
          
          class OtaThread
          {
          public:
              static OtaThread *getInstance() ;
          
              void initImpl();
          
              int initAndStartThread() ;
              void stopThread();
              void checkForOTAThread();
              OtaThreadState runImpl(OtaThreadState) ;
              pthread_t _thread;
              int getBackSyncTimeout();
              static boomer::transaction::TransactionResult SyncTimeOutCallback(void *NotUsed, int argc, char**argv, char** azColName);
          private:
              OtaThread();
              ~OtaThread();
              static void *runThreadFunc(void *This) ;
              void run(void) ;
          
              Event mEvent;
              int OtaTimer,defaultTimer;
              int deviceTimer;
              static OtaThread *otaThreadInstance;
              static bool otaThreadActive;
              static int NumberofOTAcall;
          
          };
          
          } /*namespace otathread*/
          
          } /*namespace app*/
          #endif /* SRC_OTATHREAD_H_ */
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #3

          @Qt-embedded-developer said in When to use static instance of class in c++ ?:

          I am Not knowing the why they use the static instance of class.

          This implementation uses static to implement the class a singleton class.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          Q 1 Reply Last reply
          4
          • KroMignonK KroMignon

            @Qt-embedded-developer said in When to use static instance of class in c++ ?:

            I am Not knowing the why they use the static instance of class.

            This implementation uses static to implement the class a singleton class.

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #4

            @KroMignon can we deallocate memory for singleton class which access database ?

            jsulmJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @KroMignon can we deallocate memory for singleton class which access database ?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Qt-embedded-developer said in When to use static instance of class in c++ ?:

              can we deallocate memory for singleton class which access database ?

              Yes, you can

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved