....and apart from it being 'hidden' outside the class, it is still global data.
Actually, being hidden outside of the class is what makes it not global data.
....and apart from it being 'hidden' outside the class, it is still global data.
Order of construction isn't what we're talking about, the fact is that the C::n var is still global data, and apart from it being 'hidden' outside the class, it is still global data.
Static data almost always means something is bad. Rare exceptions exist. Types containing static collections of pointers to instances of self is a dirt common OO failure/antipattern.
This does not violate encapsulation principles because the operation is hidden from outside the class, and has no bearing on the result of any publically accessable function calls on the instance. So you can construct any number of these objects and use them, and it doesn't matter which object did something special in response to global state as they all behave the same from the perspective of the calling code.
[/quote]
I don't entirely buy this. What happens if I, or worse still another library that I might not be thinking about, likewise tries to call these special functions. Maybe I try to initialise these Windows subsystems differently Perhaps the two seemingly independent libraries now interact unusually together, preventing the program from starting up or causing confusing errors.
If I want to unit test this behaviour, how can I? I cannot mock out the library initialisation code and force it to return various errors, thus testing the different code paths.
I don't entirely buy this. What happens if I, or worse still another library that I might not be thinking about, likewise tries to call these special functions. Maybe I try to initialise these Windows subsystems differently Perhaps the two seemingly independent libraries now interact unusually together, preventing the program from starting up or causing confusing errors.[/quote]
You're more or less at the mercy of the operating system here. No coding convention can prevent other people's code from calling the same functions directly, but it's possible to prevent instances of the same class from doing so. The problem with trying to eliminate global state in this situation is that the operating system itself employs global state.
If I want to unit test this behaviour, how can I? I cannot mock out the library initialisation code and force it to return various errors, thus testing the different code paths.[/quote]
You can't, but containing the global state to the private portion of the object ensures the object can be used without spreading it's global state into whatever code creates an instance of it. Barring any bugs directly related to the initialisation process of course.