template<typename T>class Singleton{public: static T& GetInstance(){static T s; return s;}protected: Singleton(){} Singleton(const Singleton&){} Singleton& operator=(const Singleton&){} ~Singleton(){}};class Derived : public Singleton<Derived>{public: void Blah(){}};
Here''s how you use it:
Derived &d = Derived::GetInstance();d.Blah();orDerived::GetInstance().Blah();
Ok, here is the creation of a chain of what I had in mind:
template<typename T>class Singleton{public: static T& GetInstance(){static T s; return s;}protected: Singleton(){} Singleton(const Singleton&){} Singleton& operator=(const Singleton&){} ~Singleton(){}};template<typename T>class Derived : public Singleton<T>{public: Derived(int _i):i(_i){} int i;};class Person : public Derived<Person>{public: Person():Derived<Person>(33){} void Print(){cout << i << endl;}};int main(){ Person::GetInstance().Print();}