This is an old revision of the document!


Singleton Template (C++)

License

Download

Before downloading or using this product, make sure you understand and accept the terms of the license.

After downloading, make sure to follow the how to use instructions below; they're worth reading.

FIXME

About

FIXME

How to Use

Setup

  • First, copy the header into your project.
  • Next, include the header somewhere in your project or precompiled header if you use one.
#include "singleton.h"
  • Create your own singleton class, ensuring that:
  1. You inherit from tjgrant::singleton<MyClass>, where MyClass is the name of your class
  2. Your constructor's access method is protected
  3. Your destructor is virtual
  4. You have the friend class statement similar to the example below
class MyClass
: public tjgrant::singleton<MyClass>
{
protected:
    //Members go here
 
protected:
    MyClass();
public:
    virtual ~MyClass();
 
public:
    //Methods go here
 
    friend class tjgrant::singleton<MyClass>;
};

Your MyClass class will now have a ::getInstance() method, which you can use to construct the class or get it's instance at runtime.

Usage

  • Construct the instance like so:
MyClass::getInstance();
  • Get the instance of the class at any point in time like so and you can do work with it:
MyClass* c = MyClass::getInstance();
 
if (c)
{
    c->doStuff(); // Assuming you have a method called ::doStuff, this is how you'd call it
}
  • You can also call methods directly without using a temporary / local variable:
MyClass::getInstance()->doStuff();

That's it! You should be good to go.

Q & A

If you have a question not answered here, please feel free to contact me and ask.

  • Q: My class already inherits from another class; can I still use this?
  • A: Yes; in fact this is generally the case
    • C++ allows for multiple inheritance so this not a problem; just comma-separate your inheritance classes and you're set
    • As an example, say your class MyClass originally inherited from MyBase; you would still follow the example above in setup with just a minor change at the beginning:
class MyClass
: public MyBase,
  public tjgrant::singleton<MyClass>
  • Q: Is this library thread safe? (Advanced: Won't apply to most users.)
  • A: It's Conditionally Safe
    • The first time you call ::getInstance, memory will be written; this is not thread-safe so your first call to ::getInstance should be on the main thread
    • After the first time, ::getInstance only reads memory, so this should be safe on any thread
  • Note: That said, I would suggest only accessing ::getInstance on the main thread whenever possible
  • Q: Singletons / Templates / Multiple Inheritance is bad and poor programming practice!
  • A: I disagree. Tools are tools, you're free to use them or not use them as you wish.
  • Note: If you really do feel this way, realize that these are your "personal politics" and I have little interest in discussing them.

If you've found this project useful, please donate today! :-)

History

November 15, 2015

  • First public release
Print/export
QR Code
QR Code Singleton Template (C++) (generated for current page)