1

I have implemented Singleton class method hpp and cpp like the following

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

My problem is how to access those methods either set or get in application classes. Please help,

Sunseeker
  • 1,503
  • 9
  • 21
Kareem Waheed
  • 429
  • 2
  • 6
  • 13

1 Answers1

2
Singleton::instance().setMemberField(42);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
nullptr
  • 11,008
  • 1
  • 23
  • 18