dimanche 23 avril 2017

share instance of a class between many source file

i'm facing the following problem:

i need to share a single instance of this class between many source files:

class Cleaner{
  static std::vector<GLuint*> vao_handler;
 public:
  static Cleaner& getInstance()
  {
    static Cleaner instance;
    return instance;
  }
  static void push_vao(GLuint &vao);
  static void clean();
 private:
  Cleaner(){}        
};

and I really don't know how to do that: i tried to declare an instance inside an header file as follow :

//global.h
#ifndef GLOBAL_H
#define GLOBAL_H
#include"Cleaner.h"

extern Cleaner instance;

#endif

and the definition..

//global.cpp
#include"global.h"

Cleaner cleaner = Cleaner::getInstance();

and then i included "global.h" in all the cpp files that need to know cleaner; but doing so , i obtain the multiple definition error ! it seems like the extern declaration is not working properly, but i have other extern variables declared/defined in global.h/global.cpp and for those others the compiler does not complain. Have you some ideas on how to work around this problem? thanks

Aucun commentaire:

Enregistrer un commentaire