This is a follow on from another question (https://stackoverflow.com/questions/10712659/c-class-design-for-monte-carlol-simulation)
I am planning on implementing a statistical distribution class, using templates. I want to make Distribution a property of an Entity class. This Distribution class can take a few different forms - TriangleDistribution, NormalDistribution and WeightedDistribution, but these are only known at runtime. They share most methods, but each type may have some custom methods too eg. setMean for NormalDistribution, setWeights for WeightedDistribution.
As I understand it, C++ templates refer to a type, which is then used to determine which implementation to use. It was suggested that I implement the different distribution types using templates.
While I think I understand the concepts of C++ templates, I'm not sure how I would go about implementing them to solve this Distribution problem. Do I use template specialialization create something like the following?:
template <WeightedDistribution>
class Distribution {
WeightedDistribtion wd;
public:
Distribution () {}
double sample () {
// Custom implementation of sample
// for weighted distribution
}
};
// class template specialization:
template <>
class Distributionr <NormalDistribution> {
NormalDistribtion nd;
public:
Distribution () {}
double sample ()
{
// Custom implementation of sample for
// a normal distribution
}
};
This would necessitate creating numerous types for each distribution type. TIA guys. Pete