Singleton in a DLL?

前端 未结 1 499
耶瑟儿~
耶瑟儿~ 2021-01-21 19:59

So i am trying to export somethings in a project to DLL. Anyways a few of the projects use a singleton class very heavily.

template 
class DLL         


        
1条回答
  •  梦毁少年i
    2021-01-21 20:37

    Please see Multiple Singleton Instances

    You will have to ensure that your template instantiation is done in one compilation unit, and you will have to move the pointer = NULL initialization to the CPP file. In other DLLs, you'll have to use extern templates.

    Edit: If you are stuck with getting templated singletons to work over multiple DLLs, you could also define a short wrapper function that returns your singleton instance so that the template instantiation is done in one compilation unit only.

    Example:

    template class Singleton;
    __declspec(dllexport/dllimport) T& getInstanceForMyType();
    // in the cpp file:
    T& getInstanceForMyType()
    {
        return Singleton::getInstance();
    }
    

    0 讨论(0)
提交回复
热议问题