Difference between revisions of "Test"
| Line 1: | Line 1: | ||
| − | <source lang=" | + | === C++ === | 
| − | //  | + | <source lang="cpp"> | 
| − | //  | + | |
| − | + | template<class T> | |
| − | + | class Singleton | |
| − | + | { | |
| − | + | public: | |
| − | } | + |     // Guaranteed to be destroyed. | 
| + |     // Instantiated on first use. | ||
| + |     static T& GetInstance() { static T instance; return instance; } | ||
| + | private: | ||
| + |     Singleton(); | ||
| + |     ~Singleton(); | ||
| + |     Singleton(const Singleton &) {}; // Don't Implement | ||
| + |     Singleton& operator=(const Singleton<T>&) {}; // Don't Implement | ||
| + |      Singleton* operator&() {}; | ||
| + | }; | ||
| + | class A : public Singleton<A> | ||
| + | { | ||
| + | friend class Singleton<A>; | ||
| + | private: | ||
| + |      A(); // Should be private | ||
| + |      ~virtual A(); | ||
| + |     A(const A&); | ||
| + |     A& operator=(const A&); | ||
| + |     A* operator&(); | ||
| + | }; | ||
| </source> | </source> | ||
Revision as of 10:14, 16 October 2014
C++
template<class T> class Singleton { public: // Guaranteed to be destroyed. // Instantiated on first use. static T& GetInstance() { static T instance; return instance; } private: Singleton(); ~Singleton(); Singleton(const Singleton &) {}; // Don't Implement Singleton& operator=(const Singleton<T>&) {}; // Don't Implement Singleton* operator&() {}; }; class A : public Singleton<A> { friend class Singleton<A>; private: A(); // Should be private ~virtual A(); A(const A&); A& operator=(const A&); A* operator&(); };
