When to use static instance of class in c++ ?
-
wrote on 18 Feb 2022, 12:18 last edited by
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_ */
-
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_ */
wrote on 18 Feb 2022, 12:24 last edited by@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.
-
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_ */
wrote on 18 Feb 2022, 12:21 last edited by@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.
-
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_ */
wrote on 18 Feb 2022, 12:24 last edited by@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.
-
@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.
wrote on 21 Feb 2022, 11:54 last edited by@KroMignon can we deallocate memory for singleton class which access database ?
-
@KroMignon can we deallocate memory for singleton class which access database ?
@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
1/5