In this question, it's advocated to use non-class functions: Using non-member methods in C++
It's also quite common to see variables in the CPP file that are local to that file module, global vars local to that compilation unit, these can be initialized in-place
But what if I want to have an init-function in my CPP file, that serves the same purpose.
e.g. a contrived simple example
int x = 6; //this is initialized at program startup
int y = 8;
Compared to
int x,y;
void Init() //I want this to be called at startup
{
//in practice this would do some more complicated stuff that isn't just init a var
x=6;
y=8;
}
How can I get Init()
called at startup?