该代码可在Windows和Linux上完美运行,但在Android中请勿调用appCfg ::〜appCfg()。如何修改代码以在所有平台上自动(在封闭程序上)销毁appCfg?

.cpp

appCfg * appCfg::p_instance = 0;
SingletonDestroyer appCfg::destroyer;

SingletonDestroyer::~SingletonDestroyer() {
    delete p_instance;
}
void SingletonDestroyer::initialize( appCfg* p ) {
    p_instance = p;
}

appCfg& appCfg::getInstance() {
    if(!p_instance)     {
        p_instance = new appCfg();
        destroyer.initialize( p_instance);
    }
    return *p_instance;
}

appCfg::appCfg()
{
    pSetting = new QSettings(Const::SettingPath()+"/main.cfg",QSettings::IniFormat);
}
//called on Windows and Linux platforms, but not on Android
appCfg::~appCfg()
{
    pSetting->sync();
    delete pSetting;
}

...

最佳答案

所有代码都不适合,因此我将其余代码写在这里

。H

class SingletonDestroyer
{
private:
    appCfg* p_instance;
public:
    ~SingletonDestroyer();
    void initialize( appCfg* p );
};

class appCfg : public QObject
{
private:
    static appCfg* p_instance;
    static SingletonDestroyer destroyer;

    QSettings * pSetting;
protected:
    appCfg();
    appCfg( const appCfg& );
    appCfg& operator=( appCfg& );
    ~appCfg();
    friend class SingletonDestroyer;
public:
    static appCfg& getInstance();
   ...
};

08-27 01:25