Singleton in a DLL?

So I'm trying to export something to a project in a DLL. In any case, some of the projects make very heavy use of the singleton class.

template <typename T>
class DLL_IMP VA_Singleton {
protected:
    VA_Singleton () {};
    ~VA_Singleton () {};
public:
    static T *Get(){
        return (static_cast<T*> (a_singleton));
    }
    static void Delete(){
        if(a_singleton == NULL) {
            delete a_singleton;
        }
    }
    static void Create(){
        a_singleton = GetInstance();
        if(a_singleton == NULL){
           a_singleton = new T;
        }
    }
private:
    static T *a_singleton;
};

template <typename T> T *VA_Singleton<T>::a_singleton = NULL;

I got the export working fine, but when it comes to import, it says the following:

template <typename T> T *VA_Singleton<T>::a_singleton = NULL;

Doesn't work with DLLImport. This is the first time I've really worked with a DLL in a production environment. Does anyone have any ideas?

+2
source share
1 answer

See Multiple Singleton Instances

, , pointer = NULL CPP. DLL extern.

Edit: DLL, -, singleton, .

:

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

All Articles