vendredi 21 octobre 2016

Error in Singleton pattern implementation

I see errors like

src/singleton.cxx:16:error: invalid use of member 'Singleton::instance' in static member function src/singleton.cxx:28:error: from this location src/singleton.cxx:16:error: invalid use of member 'Singleton::instance' in static member function src/singleton.cxx:29:error: from this location src/singleton.cxx:16:error: invalid use of member 'Singleton::instance' in static member function src/singleton.cxx:31:error: from this location src/singleton.cxx: In function 'int main()':

#include <cstddef>

class Singleton {

public :
  Singleton * instance;
  static Singleton * get_instance();


private:
  Singleton();
  int m_num;
  int incr_call();

};

Singleton * Singleton::get_instance() {
  if (instance == NULL) {
    instance = new Singleton();
  }
  return instance;
}

Singleton::Singleton():
  m_num(0)
{
  instance = NULL;
}

int Singleton::incr_call() {
  return m_num++;
}

int main() {
  Singleton * s = Singleton::get_instance();
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire