I think this would work if I could just figure out how to initialize the static value to false a single time at the start of the program.
// my_class.h
class my_class {
public:
// ...
private:
static bool seeded;
};
// my_class.cpp
bool my_class::seeded = false;
Make sure to define seeded
in the implementation file. Otherwise every file which includes your header will get its own definition of the static member and it could also cause linker issues as it can be defined more than once.
On a side note, if the static member were of a const integral type, you could assign it at the point of declaration.
Another option would be this, and personally I would prefer it for this task:
my_class::my_class()
{
static bool seeded = false;
if(!seeded) {
srand(time(NULL));
seeded = true;
}
myVariable = rand() % maxVal;
}