Singleton Class in C++

前端 未结 3 1628
南方客
南方客 2020-12-17 05:59

I have used singleton calss following the example:

singleton class

But i get the error as \"Unresolved external symbols\"

this is th

3条回答
  •  情话喂你
    2020-12-17 06:37

    In your definition file, you need the definition of instance:

    singleton* singleton::instance = NULL;
    

    You should separate your definition from your declaration if you want to use the singleton in multiple translation units.

    Also, the way it's usually done, is not having an initialize method:

    static singleton* getInstance()
    { 
        if(instance==NULL)
            instance = new singleton();
        return instance;
    }
    

    There are lots of discussions on SO whether singletons are good or bad, with the general consensus that they should be avoided. You should also check them out.

提交回复
热议问题