Line 1: Line 1:
<source lang="javascript" collapse="true" first-line="2">
+
=== C++ ===
// SyntaxHighlighter makes your code snippets beautiful without tiring your servers.
+
<source lang="cpp">
// http://alexgorbatchev.com
+
 
var setArray = function(elems) {
+
template<class T>
     this.length = 0;
+
class Singleton
     push.apply(this, elems);
+
{
     return this;
+
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++

  1. template<class T>
  2. class Singleton
  3. {
  4. public:
  5. // Guaranteed to be destroyed.
  6. // Instantiated on first use.
  7. static T& GetInstance() { static T instance; return instance; }
  8. private:
  9. Singleton();
  10. ~Singleton();
  11. Singleton(const Singleton &) {}; // Don't Implement
  12. Singleton& operator=(const Singleton<T>&) {}; // Don't Implement
  13. Singleton* operator&() {};
  14. };
  15. class A : public Singleton<A>
  16. {
  17. friend class Singleton<A>;
  18. private:
  19. A(); // Should be private
  20. ~virtual A();
  21. A(const A&);
  22. A& operator=(const A&);
  23. A* operator&();
  24. };