dI am programming a simulation. Now it should both work with 2D and 3D, so I’m trying to make my classes work with 2D- and 3D-Vectors. Also the Vectors should have an template parameter to indicate which type should be used for the coordinates.
My base class looks like this:
class SimulationObject {
AbstractVector<int>* position;
AbstractVector<float>* direction;
}
Now the problem is, I can’t use polymorphism, since then all my Vectors would have to be pointers and this makes operator-overloading nearly impossible for operations like this:
AbstractVector<float>* difference = A.position - (B.position + B.direction) + A.direction;
But also I can’t use a template parameter to specify which type to use:
template <typename T> class Vector2d;
template <typename T> class Vector3d;
template <class VectorType> class SimulationObject {
VectorType<int> position;
VectorType<float> direction;
}
SimulationObject<Vector2D> bla;
//fails, expects SimulationObject< Vector2D<int> > for example.
//But I don't want to allow to specify
//the numbertype from outside of the SimulationObject class
So, what do?
You can use template template parameters: