Saturday 15 September 2012

Calling functions of a static member object in C++ -


I'm not sure that I have used the correct terminology in my headline, but what I'm trying to do here :

I have a light particle class (simplified) for which random behavior is required for birth and wall collision. I want to keep the facilities in the classroom. For my understanding, a stable member is created only once and can be shared in all instances of class.

For the random number generator object, I would like to call the seed method once, but it is uncertain how to do it, because most examples are plain fixed variables or functions Use.

Particle: h

  #include & lt; Random & gt; Class particle (public: particle); private: static std :: default_random_engine pRNG; static std :: uniform_real_distribution   

< Strong> particle. CPP

  #include "particle.h" #include & lt; ctime & gt; Std :: default_random_engine particle :: pRNG.seed (time) ); // Wrong - help! Particle :: particle () {} // methods, etc.   

By inserting the seed method in the particle manufacturer I think it is called on every birth particle. The only accelerated hack which I could think was to add a Boole member to the particle orbit, which lies on the first seed call

An easy way to do this: create a category named "AutoInitRNG", which is its constructor In the default_random_engine , there are seeds and make the static member variables of the particle something like this:

  AutoCit; ) {PRngs.seed (time (zero));}}; square particle {// ... private: static AutoInitRNG RNG; };    

No comments:

Post a Comment