1

dシミュレーションをプログラミングしています。これで、2Dと3Dの両方で機能するはずなので、クラスを2Dと3Dのベクトルで機能させようとしています。また、Vectorsには、座標に使用するタイプを示すテンプレートパラメータが必要です。

私の基本クラスは次のようになります。

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

それで、何をしますか?

4

1 に答える 1

3

テンプレートテンプレートパラメータを使用できます。

template <template <class> class VectorType> 
class SimulationObject {
    VectorType<int> position;
    VectorType<float> direction;
};
于 2012-05-26T17:52:07.100 に答える