Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- template <typename T>
- class Singleton
- {
- protected:
- static T* instance;
- Singleton() {}
- Singleton(const Singleton&);
- Singleton& operator=(const Singleton&);
- public:
- static T* getInstance()
- {
- if (!instance)
- instance = new T();
- return instance;
- }
- };
- template <typename T>
- T* Singleton<T>::instance = nullptr;
- class MyClass : public Singleton<MyClass>
- {
- public:
- MyClass(){}
- string name;
- };
- int main() {
- MyClass *mc = nullptr;
- mc = MyClass::getInstance();
- mc->name = "andrew";
- MyClass *oc = mc;
- cout << oc->name << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement