C++单例模式及其析构

utilities.h

=====================================================

#ifndef UTILITIES_H
#define UTILITIES_H

#include

class Utilities
{
public:
    static QMutex *getRegisterMutex();

private:
    Utilities();

private:
    static QMutex *m_mutex;
    static int     m_fd;

    class Garbo
    {
    public:
        ~Garbo()
        {
            if (Utilities::m_mutex)
            {
                delete Utilities::m_mutex;
            }

        }

};


    static Garbo m_garbo;
};

#endif // UTILITIES_H


utilities.cpp

=====================================================

#include "utilities.h"

QMutex *Utilities::m_mutex = 0;

Utilities::Utilities()
{
}

QMutex* Utilities::getRegisterMutex()
{
    if (!m_mutex)
    {
        m_mutex = new QMutex();
    }

    return m_mutex;
}

你可能感兴趣的:(Qt)