访问blackberry中的单例类方法10

时间:2013-08-07 06:12:22

标签: c++ singleton blackberry-10 blackberry-cascades

我已经实现了Singleton类方法hpp和cpp,如下所示

Singleton.hpp

    class Singleton {
private:
    Singleton();

public:
    virtual ~Singleton();
    static Singleton &instance();

    int getMemberField();
    void setMemberField(int mf);

private:
    static Singleton    *p_instance;

    int                 m_memberField;
};

Singleton.cpp

Singleton* Singleton::p_instance=NULL ;

Singleton::Singleton() {
    p_instance = this;
    m_memberField = 0;
}

Singleton::~Singleton() {
    p_instance = NULL;
}

Singleton&    Singleton::instance() {
    if (p_instance==NULL) {
        p_instance = new Singleton();
    }
    return *p_instance;
}
int Singleton::getMemberField(){
    return m_memberField;
}

void Singleton::setMemberField(int mf){
     m_memberField = mf;
}

我的问题是如何访问设置或获取应用程序类的方法。 请帮忙,

1 个答案:

答案 0 :(得分:2)

Singleton::instance().setMemberField(42);