Being way less than gifted with artistic skills, I won't torture
people with any sketches I've done (no, I'm not being modest,
I really _do_ suck at art [;)])
Instead, here's some code I wrote that implements a reasonably
easy-to-use (and IMHO rather nifty) singleton template class...
Stuff to go in the header file "autosing.hxx":
[code]#ifndef AUTOSING_H
#define AUTOSING_H
template
class AutoSingleton
{
public:
AutoSingleton() {}
~AutoSingleton()
{
delete mptSingleton;
}
static Tx * instance()
{
if (mptSingleton == NULL)
mptSingleton = new Tx();
return mptSingleton;
}
private:
static Tx * mptSingleton;
};
#define SingletonInstance(Type) AutoSingleton::instance()
#define DefineSingleton(Type) AutoSingleton AS_ ## Type; Type * AutoSingleton::mptSingleton = NULL
#define DeclareSingleton(Type) extern AutoSingleton AS_ ## Type
#endif /* AUTOSING_H */[/code]To use it, add this to your class's header:
[code]class MonsterGenerator
{
public:
// ...
};
DeclareSingleton(MonsterGenerator);[/code]Then to your class's implementation, add:
[code]MonsterGenerator::MonsterGenerator()
{
// ...
}
DefineSingleton(MonsterGenerator);[/code]Then finally to use it in your code, include the header and do the following:
[code]int main()
{
Monster * ptMonster = SingletonInstance(MonsterGenerator)->generateMonster();
return 0;
}
[/code]Got some other geeky code samples? Post 'em ;)
Cheers,
CombatWombat the exhibitionist (umm, that didn't quite come out right, did it [;)])
I agree! (yay for mods!)
Hurrah!