4 ms·
If one isn’t careful, you end up with interdependency between static initializers. Since the order of static initialization is undefined, you get fun bugs like
by jmaygarden 9y ago
If one isn’t careful, you end up with interdependency between static initializers. Since the order of static initialization is undefined, you get fun bugs like your program crashing because a minor change caused the objects to link in a different order.
For example, the dreaded singleton using a static variable inside a function:
Singleton& get_singleton() {
static Singleton instance;
return instance;
}
Having a couple of those referenced in static initializers is a recipe for disaster. It’s a bad practice, but people unfortunately do it all the time. Those that do this are equally unequipped to realize why their program suddenly started crashing after an innocuous change.
- jensa1948 9y agoPeople who use singletons deserve no better...