Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Singleton Class Example
Forum Updated to NodeBB v4.3 + New Features

Singleton Class Example

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 22.6k Views 2 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.
  • A Offline
    A Offline
    AthLazaros
    wrote on last edited by
    #1

    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
    0
    • mistralegnaM Offline
      mistralegnaM Offline
      mistralegna
      wrote on last edited by mistralegna
      #2

      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
      2
      • A Offline
        A Offline
        AthLazaros
        wrote on last edited by AthLazaros
        #3

        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
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          1

          • Login

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