0
C++で抽象基底クラスを定義する必要があります。抽象基底クラスは、そのノードにあらゆるタイプのデータを保持できます。派生クラスの機能を拡張します。ここでは、保持する表現の種類(MatrixまたはList)を指定します。 EXのためにどのような種類のデータをそのノードに保持するための抽象的なGraphクラスを定義するには?
:
template<class T>
class Graph // <---- This is the abstract base class
{
public:
virtual void addEdge(u32 src, u32 dest, T value) = 0 override;
virtual void display() = 0 override;
virtual unsigned int getVertices() = 0 const override;
Graph() { };
virtual ~Graph() { };
};
template<class T>
class AdjListGraph::public Graph
{
//This is the derived class representing graph as a adj list
}
template<class T>
class AdjListMatrix::public Graph
{
//This is the derived class representing graph as a adj matrix
}
最後に、私は以下のようにそれを使用したい:
Graph<char> *g = new AdjListGraph<char>(10);
g->addEdge(0,1,'c');
Graph<int> *g = new AdjListMatrix<int>(10);
g->addEdge(0,1,100);
クラスを継承するとき私がいる問題があります。基本クラスはテンプレートなので、継承するときにクラスの型を指定する必要があります。しかし、これには複数のタイプのデータに対して同じコードを書き直す必要があります。さらに、カスタムオブジェクトをグラフに格納すると、各タイプの特殊化を期待するのは面倒です。
この制限を回避する方法はありますか?