Original Post
Hi, I've implemented a simple singleton solution as shown below: Using this solution, I'm curious about the implications of using this across DLLs, for example: If I were to compile the above code into a single library DLL, would external usage of the MyClass singleton access the library version or create it's own copy of the static variable? Thanks in advance. EDIT: Changed the Singleton class code a little. [Edited by - auron777 on August 11, 2009 7:17:26 AM]
// Singleton.h
template< typename T >
class Singleton
{
public:
Singleton();
~Singleton();
static T & Instance( void )
{
return mSingleton;
}
static T * InstancePtr( void )
{
return &mSingleton
}
protected:
static T mSingleton;
private:
Singleton( Singleton const & ); // Hide copy ctor
Singleton & operator = ( Singleton const & ); // Hide assignment operator
};
template< typename T >
T Singleton< T >::mSingleton = T();
class __declspec( dllexport )
MyClass : public Singleton< MyClass >
{
public:
void Print( void )
{ printf( "Hello!" ); }
};
#include "MyClass.h"
...
MyClass::Instance().Print();