Skip to content

Instantly share code, notes, and snippets.

@mhseiden
Last active December 11, 2015 17:58

Revisions

  1. mhseiden revised this gist Jan 25, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Singleton Example
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    // NOTE - I haven't compiled this, so there may be small errors. However, the concepts shown are correct.
    /**
    * NOTE - I haven't compiled this, so there may be small errors.
    * However, the concepts shown are correct.
    */

    // In the *.h file
    class MyClass;
  2. mhseiden created this gist Jan 25, 2013.
    27 changes: 27 additions & 0 deletions Singleton Example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // NOTE - I haven't compiled this, so there may be small errors. However, the concepts shown are correct.

    // In the *.h file
    class MyClass;

    class MyClass {
    public:
    /** Static Singleton accessor. Use a ref for easy access and use **/
    static MyClass& get();

    /** Instance Methods **/
    // ...

    private:
    MyClass(); // Constructor
    ~MyClass(); // Destructor
    // Could hide equals operator and copy constructor...

    /** Other private data **/
    // ...
    };

    // In the *.c file
    static MyClass& MyClass::get() {
    static MyClass singleton;
    return singleton;
    }