Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Singleton Class Example

    General and Desktop
    3
    4
    19371
    Loading More Posts
    • 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.
    • A
      AthLazaros last edited by

      Good evening
      Can somebody give me an example of a Sigleton class using a static qreal and a static Qstring (Determination and Implementation)
      Thank you in advance

      1 Reply Last reply Reply Quote 0
      • mistralegna
        mistralegna last edited by mistralegna

        Hello !

        A basic implementation of the Singleton pattern could be:

        class Singleton
        {
            private:
                Singleton() {}
                Singleton(const Singleton&) {}
                Singleton& (const Singleton&) {
                    return *this;
                }
        
               // In C++11, copy constructor and assignment operator could be deleted (= delete) 
        
            public:
                static Singleton & instance() {
                    static Singleton * _instance = 0;
                    if ( _instance == 0 ) {
                        _instance = new Singleton();
                    }
                    return *_instance;
                }
        };
        

        Warning: this implementation is not thread-safe!!! If you want a thread-safe one, you can read this link (to an article on the Wiki) : https://wiki.qt.io/Qt_thread-safe_singleton

        I didn't show any of your qreal or QString because I don't know how you want to ue them,

        Do not hesitate to ask your questions!

        1 Reply Last reply Reply Quote 2
        • A
          AthLazaros last edited by AthLazaros

          Thank you very much for your response
          The problem is that i have code of a singleton class using qstring and another code using a singleton using qstring, i preety much understand the implemention and determination but i dont know how to compine them.
          Here is the code
          //Definition of singleton a singleton class with qreal variables
          #ifndef CTL_VARS_H
          #define CTL_VARS_H

          #include <QObject>

          class CTL_vars : public QObject
          {
          Q_OBJECT
          public:
          explicit CTL_vars(QObject *parent = 0);
          static CTL_vars * Instance();
          static qreal a;
          static qreal b;
          signals:

          public slots:

          private:
          static CTL_vars * mp_Instance;

          };

          #endif // CTL_VARS_H

          //Implementetion of singleton class with qreal variables
          #include "ctl_vars.h"
          //TKM
          qreal CTL_vars::a=0;
          qreal CTL_vars::b=0;
          CTL_vars * CTL_vars::mp_Instance=NULL;
          CTL_vars::CTL_vars(QObject *parent) :
          QObject(parent)
          {
          }

          CTL_vars *CTL_vars::Instance()
          {
          if (!mp_Instance)
          {
          mp_Instance=new CTL_vars;
          }
          return mp_Instance;
          }

          The folowing code uses a qstring

          #ifndef CTL_VARS_H #define CTL_VARS_H
          #include <QObject>
          class CTL_vars : public QObject { Q_OBJECT public:
          ~CTL_vars(); static CTL_vars* Instance(); signals:
          public slots:
          public: static QString m_string; void set_mstring(void);
          private: explicit CTL_vars(QObject *parent = 0); static CTL_vars *mp_Instance;
          };
          #endif // CTL_VARS_H
          //CTL_VARS singleton class #include "ctl_vars.h"
          CTL_vars *CTL_vars::mp_Instance=NULL; QString CTL_vars::m_string="Hello Man";
          CTL_vars::CTL_vars(QObject *parent) : QObject(parent) {
          }
          CTL_vars::~CTL_vars() {
          }
          CTL_vars *CTL_vars::Instance() { if (!mp_Instance) { mp_Instance=new CTL_vars; } return mp_Instance; }
          void CTL_vars::set_mstring() { CTL_vars::m_string="Change String"; }

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion last edited by

            If I understand you right you want to have one singleton with the static variables from both classes?
            Just take one of these classes put the static variables from the other into it and you're done.
            For example take CTL_vars with qreal and add the m_string variable from the other.

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

            1 Reply Last reply Reply Quote 1
            • First post
              Last post