Skip to content

Instantly share code, notes, and snippets.

@vivekgalatage
Last active December 16, 2015 06:49
Show Gist options
  • Save vivekgalatage/5394418 to your computer and use it in GitHub Desktop.
Save vivekgalatage/5394418 to your computer and use it in GitHub Desktop.
Singleton Template ass base class
template <class T>
class SingleTon
{
public:
static T& instance() {
static T inst;
return inst;
}
protected:
SingleTon(){}
SingleTon(const SingleTon&){}
SingleTon& operator=(const SingleTon&){}
~SingleTon(){}
};
#define MAKE_SINGLETON(Class) \
friend class SingleTon<Class>; \
private: \
Class() {} \
Class(const Class&) {} \
Class& operator=(const Class&); \
~Class() {} \
class PrintSpooler : public SingleTon<PrintSpooler>
{
MAKE_SINGLETON(PrintSpooler);
public:
void printMe() {
// Do Something
}
};
int main()
{
PrintSpooler& p = PrintSpooler::instance();
p.printMe();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment