Original Post
So I got me awesome Game Progamming Gems Vol 1 book and bam, there is a technique very relevant to what I need in my current project - singletons. I guess I could do the simple way of implementing them and rewrite the code for every class. But I know that was is crap. So I wanna use the template method. Only I don''t really understand what the code int the book means exactly...
The biggest part I dont get is the constructor definition:
Also I don''t really understand the last line. I think it''s just initializing the singleton pointer to null, but the syntax need to sink in.
"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
template <typename T> class Singleton
{
static T* ms_Singleton;
public:
Singleton(void) //This is the part I really don''t get
{
assert(!ms_Singleton);
int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
ms_Singleton = (T*)((int)this + offset);
}
};
template <typename T> T* Singleton<T>::ms_Singleton = 0;