lundi 5 décembre 2016

Singleton pattern for derivable class

In the normal singleton pattern, the singleton class is effectively "sealed" (cannot be derived):

class A
{
private:
    static A m_instance; // instance of this class, not a subclass

    // private ctor/dtor, so class can't be derived
    A();
    ~A();

public:
    static A& GetInstance() { return m_instance; }

    ...
};

How would you write a class that is meant to be derived, but whose derived class should only be instantiated once?

Aucun commentaire:

Enregistrer un commentaire