1

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

单例.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;
};

单例.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;
}

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

4

1 回答 1

2
Singleton::instance().setMemberField(42);
于 2013-08-07T06:15:04.337 回答