We know that Meyers Singleton is thread safe since c++11.
I'm using this singleton pattern.
struct singleton_t
{
static
singleton_t &
instance()
{
static singleton_t s;
return s;
} // instance
singleton_t(const singleton_t &) = delete;
singleton_t & operator = (const singleton_t &) = delete;
private:
singleton_t() {}
~singleton_t() {}
}; // struct singleton_t
singleton_t is not a pointer or reference so the polymorphism doesn't apply.
Now I want to do it something like this:
struct singleton_t
{
static
singleton_t *
instance(singleton_t* p = nullptr)
{
static singleton_t *s = nullptr == p ? new singleton_t : p;
return s;
} // instance
singleton_t(const singleton_t &) = delete;
singleton_t & operator = (const singleton_t &) = delete;
private:
singleton_t() {}
~singleton_t() {}
}; // struct singleton_t
In this way, I singleton_t can point to instance of its subclass so the polymorphism can be achieved. Or is there a better approach?
Aucun commentaire:
Enregistrer un commentaire