I have a class with a static member. This will be initialized using a private static function of the same class.
#include <iostream>
#include <string>
class A
{
public:
static std::string const s;
private:
static std::string make()
{
return "S";
}
};
std::string const A::s = A::make();
int main()
{
std::cout << A::s << std::endl;
// std::cout << A::make() << std::endl; // <-- Does not work
return 0;
}
My question is: Because of which rule is this allowed? Clearly the commented part does not work, because I am not allowed to access a private function from outside the class. So why is the initialization of the private static member during startup a special case? (And on a side note: what is the intention of this rule? Is it to allow this exact case?)
I am aware of other mechanisms to initialize a static member (like here: Initializing private static members). But in my case the member is const, so as far as I know the only way to set it is via direct initalization at the place of definition.