2012-05-26 54 views
1

dI编程模拟。现在它应该可以使用2D和3D,所以我试图让我的类与2D和3D矢量一起工作。此外,矢量应该有一个模板参数,以指示应该使用哪种类型的坐标。使C++模拟类与2D和3D矢量一起工作

我的基类,看起来是这样的:

class SimulationObject { 
    AbstractVector<int>* position; 
    AbstractVector<float>* direction; 
} 

现在的问题是,我不能使用多态,从那时起我所有的载体必须是三分球,这使得运营商超载的操作几乎是不可能的像这样:

AbstractVector<float>* difference = A.position - (B.position + B.direction) + A.direction; 

但我也不能使用模板参数来指定要使用的类型:

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 

那么,该怎么办?

回答

3

您可以使用模板模板参数:

template <template <class> class VectorType> 
class SimulationObject { 
    VectorType<int> position; 
    VectorType<float> direction; 
};