In the book of "The C++ Language", the author claimed
Sometimes, when you design a library, it is necessary, or simply convenient, to invent a type with a constructor and a destructor with the sole purpose of initialization and cleanup. Such a type would be used once only: to allocate a static object so that the constructor and the destructor are called.
I am interested which kind of scenario that this statement is referring to? Or how this statement helps the software design?
The book also gives an example
class Zlib_init{
Zlib_init( );
~Zlib_init( );
};
class Zlib{
static Zlib_init x;
}
And the book states that
Unfortunately, it is not guaranteed that such an object is initialized before its first use and destroyed after its last use in a program consisting of separately compiled units.
Why this can happen?
Thanks for clarification.