With simple values, there is no problem: we simply set them in the .cpp file.
But consider the following example, that require some init/cleanup code:
// .h
class MyClass {
...
private:
static unsigned First1000Primes[1000];
} ;
In the .cpp file, we want to initialize the array, and need some code for that.
We can define a class Initializer in the .cpp file, with a ctor that fills the values, and declare a static Initializer InitAndCleanup in the same file, so the code will run once at startup (and similarly run dtor/cleanup code if needed).
The problem with this solution is its ‘messiness’, as the initializer has to be a friend of MyClass to access its private parts.
So my question is: is there a cleaner way?
I would say that like everything else, it’s just a matter of encapsulation and packaging.
And then use that:
And initialize in the cpp as usual: