In the book The C++ Programming Language, by Bjarne Stroustrup, the author says:
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. For example:
class Zlib_init { Zlib_init() ; //get Zlib ready for use ~Zlib_init() ; //clean up after Zlib }; Class Zlib { static Zlib_init x; / /... };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 does the author keep the constructor and destructor as private members?
And why won’t this method work if we use it in a program consisting of seperately compiled units?
Won’t it require definition of the member x for calling the constructor Zlib_init() and destructor ~Zlib_init()? Then what is the use of this method?
It’s in section 10.4.9 of the book.
The constructor & destructor being
privateseems to be a typo.Class
staticmembers needs to be defined in order that you can use them. In order that the static memberxis defined the constructor needs to be accessible. if not the linker will complain about undefined reference.Online Sample:
Output:
If you fix the typo mentioned above by making the constructor and destructor
publicor by makingZliba friend of classZlib_initthe code still faces another problem.The problem is popularly known as Static Initialization Fiasco in C++.
Good Read:
[10.14] What’s the “static initialization order fiasco”?
[10.17] How do I prevent the “static initialization order fiasco” for my static data members?