Static members confuse me sometimes. I understand how to initialize a simple built in type such as int
with something along the lines of int myClass::statVar = 10;
, which you place in a .cpp file, but I have something of the following sort:
class myClass
{
public:
// Some methods...
protected:
static RandomGenerator itsGenerator;
}
The basic idea is simple enough: myClass
needs access to a random generator for one of its member functions. I also can have only a few instances of the generator since each object is quite big. However, the RandomGenerator
type needs to be "initialized", so to speak, by a call to RandomGenerator::Randomize()
, which the compiler won't allow you to do since it's not a const rvalue (is that right?).
So how can I make this work?
Or perhaps should I not make use of a static variable in this case, and do it some other way?