Last active
December 16, 2015 06:49
-
-
Save vivekgalatage/5394418 to your computer and use it in GitHub Desktop.
Singleton Template ass base class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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