Is this a legit way to create a base singleton object which ensures that all it's childs are singleton ass well? I want to use this in combination with the factory pattern to ensure that all factorys are singleton.
The key class is meant to prevent childs from hacking around the singletons constructor but is basically only a formal parameter.
Is there anything wrong in particular with that approach?
class singleton
{
protected:
struct key
{
private:
friend class singleton;
key(){}
};
public:
singleton(const key&)
{}
template <class child> static child* getInstance()
{
static key instanceKey;
static child* unique = new child(instanceKey);
return unique;
}
private:
};
class test : public singleton
{
public:
test(singleton::key& key)
: singleton(key)
{}
void init()
{
//init object
}
private:
};
int main()
{
test* t = singleton::getInstance<test>();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire