I'm not an expert on singletons, and I only have one bit of code that uses it. It's part of a debugging system from one of NVIDIA's SDKs, and I find their code to be useful in my engine. Now, this singleton class works fine in Windows, but not for UNIX based OSes like MacOSX. This is what it looks like:
#include <cassert>
template <typename T> class Singleton
{
static T* ms_Singleton;
public:
Singleton( void )
{
assert( !ms_Singleton );
int offset = *(int*)(T*)1 - *(int*)(Singleton <T>*)(T*)1;
ms_Singleton = (T*)(*(int*)this + offset);
}
virtual ~Singleton( void )
{ assert( ms_Singleton ); ms_Singleton = 0; }
static T& GetSingleton( void )
{ assert( ms_Singleton ); return ( *ms_Singleton ); }
static T* GetSingletonPtr( void )
{ return ( ms_Singleton ); }
};
template <typename T> T* Singleton <T>::ms_Singleton = 0;
So, it crashes in the constructor, starting at the second line. The windows version had (int) instead of *(int*). I just changed that so XCode would stop complaining about it. It could be something simple. Right now, I'm so tired, I can barely stay awake. So, I was hoping someone who isn't as braindead as I am right now could point me in the right direction. Thanks.
Shogun.