I have a class that initializes a boost::normal_distribution
object in the constructor. How do I store this object in a member so that it is available elsewhere in the class? I think I want to store a pointer to the boost object, but once I leave the constructor, the object gets released from the stack. So, I think I really want to allocate the normal distribution object on the heap with a new
, but I'm at a loss to get the syntax right.
class Generator
{
private:
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > *_var_nor;
public:
Generator( int avg_size, int stddev_size )
PhraseGenerator( size, words );
boost::mt19937 rng; // I don't seed it on purpouse (it's not relevant)
boost::normal_distribution<> nd(avg_size, stddev_size);
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > var_nor(rng, nd);
_var_nor = &var_nor;
};
int normal_distrib_value()
{
return (*_var_nor)();
}
};