This is
almost, but not quite, a singleton. The advantages are that it doesn't
enforce only one instance of the class, it just guarantees to only
provide one itself, and it can be used for any other class, uninvasively. "The" is still basically a global variable - there is no excuse for using it to access a higher level class - I'm not saying there is. What it gives us is easy access to 1 instance of a class from anywhere and automatic lazy initialisation, but it does it without invading the class itself or preventing other instances being created. template <class T> struct The { inline T * operator->() { static T instance; return &instance; } }; usage as: The<Logger>()->log("hello"); |