Skip to content

Instantly share code, notes, and snippets.

@asukharev
Last active December 10, 2015 17:58
Show Gist options
  • Save asukharev/4471146 to your computer and use it in GitHub Desktop.
Save asukharev/4471146 to your computer and use it in GitHub Desktop.
c++ singleton pattern
//
// Singleton.h
//
// Created by Alexander Sukharev on 05.01.13.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
//
#ifndef Singleton_h
#define Singleton_h
template<class Class>
class Singleton {
public:
Singleton(void) {
if (instance_)
throw; // or what have you...
instance_ = (Class*) this;
}
static inline Class& instance(void) {
return *instance_;
}
static inline bool exists(void) {
return instance_ != 0;
}
virtual ~Singleton(void) {
instance_ = 0;
}
protected:
static Class* instance_;
};
template<typename Class>
Class* Singleton<Class>::instance_ = 0;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment