singleton - Why do I get an "Unresolved Externals" when calling a static attribute? Visual C++ -


although there lot of answers topic, still have problem.

i want, everybody, implement singleton pattern. tried this:

class factory {  private:     factory(void);     static factory* self_inst;  public:      ~factory(void);      iportio* getstrategy(int porttype);     static factory *getinstance()     {                    if(self_inst == null)             self_inst = new factory();         return self_inst;            }  }; 

the problem comes when call *self_inst* in getinstance() static method. lot of people said when using static variables, must not declare it, define somewhere else. well, i've problem trying define in place:

1 outside class:

factory* factory::self_inst; 

2 outside class, value:

factory* factory::self_inst=null; 

3 inside static method:

static factory *getinstance() {          factory* factory::self_inst;           if(self_inst == null)         self_inst = new factory();     return self_inst;        } 

4 , inside static method value:

static factory *getinstance() {          factory* factory::self_inst=null;           if(self_inst == null)         self_inst = new factory();     return self_inst;        } 

nothing works! decided not create static attribute in class, static variable inside static method. works, not same , not programming practice, since should if need access same variable 2 statics methods? it's not case, it's question , know if knows hot it.

number 2 right (actually number 1, because default initialisation null), remember put in cpp file, not in header.

if it's still not working post error message.


Comments