Singleton Class Example



  • 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



  • 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!



  • 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"; }


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.