Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit Singleton

Quick question. Is there anyway to inherit a singleton so that the child class is a singleton? I have searched around but every singleton I can find is implemented per class, and not in a generic fashion.

like image 549
44701 Avatar asked Dec 29 '15 21:12

44701


2 Answers

Yes there is a generic way. You can implement a Singleton via CRTP, like:

template<typename T>
class Singleton
{
protected:
    Singleton() noexcept = default;

    Singleton(const Singleton&) = delete;

    Singleton& operator=(const Singleton&) = delete;

    virtual ~Singleton() = default; // to silence base class Singleton<T> has a
    // non-virtual destructor [-Weffc++]

public:
    static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value)
    {
        // Guaranteed to be destroyed.
        // Instantiated on first use.
        // Thread safe in C++11
        static T instance{};

        return instance;
    }
};

then derive from it to make your child a Singleton:

class MySingleton: public Singleton<MySingleton>
{
    // needs to be friend in order to 
    // access the private constructor/destructor
    friend class Singleton<MySingleton>; 
public:
    // Declare all public members here
private:
    MySingleton()
    {
        // Implement the constructor here
    }
    ~MySingleton()
    {
        // Implement the destructor here
    }
};

Live on Coliru

like image 90
vsoftco Avatar answered Sep 22 '22 03:09

vsoftco


A singleton has a static getInstance() method that upon first invocation creates the single instance of the class, and therefore is statically-bound to the type of singleton class being instantiated. I don't see the immediate utility of having a Singleton hierarchy. But if you insist on having one, you might consider making the getInstance method virtual and overriding it in the class extending your parent Singleton.

class Singleton {
    public virtual Singleton * getInstance() const
    {
        // instantiate Singleton if necessary
        // return pointer to instance
    }
    ...
};

class MySingleton : public Singleton {
    // overrides Singelton's getInstance
    public virtual MySingleton * getInstance() const
    {
        // instantiate MySingleton if necessary
        // return pointer to instance
    }
};

Of course, a robust implementation would store and return smart pointers.

like image 41
user455495 Avatar answered Sep 21 '22 03:09

user455495