我有以下图表类层次结构:
typedef vector<int> ArrayI;
typedef vector<Array<long>> Mat2DB;
typedef vector<ArrayI> adjList;
class baseGraph {
int nodes;
ArrayI degree;
//some member functions.
}
class matGraph: public baseGraph {
Mat2DB matrix;
//member functions.
}
class lMatGraph: public matGraph {
ArrayI labels;
//member functions.
}
class listGraph: public baseGraph {
adjList list;
//member functions.
}
class lListGraph: public listGraph {
ArrayI labels;
//member functions.
}
现在在这个类中,我有许多其他函数,主要是虚拟函数,这样当我在使用基类指针时调用正确的函数时。
例如,我有一个sssp(int node)
实现单源最短路径的函数。实现是不同的class matGraph
,class listGraph
分别是图的邻接矩阵表示和邻接表表示。现在不需要更改这些图的标记版本的定义,所以我不再定义这些lListGraph
函数lMatGraph
现在我唯一遇到的问题是setLabel(const ArratI &)
在课堂上lListGraph
。lMatGraph
我需要这个函数是虚拟的,以便通过基类指针调用它,但同时我没有任何东西,例如类的标签matGraph
和listGraph
.
我不知道我的设计层次是否正确,但对我来说似乎很直观。因此,对此的任何评论都会很好。我可以用这个setLabel
功能做什么。是否可以拥有这样的功能(对我来说,这看起来像是一种解决方法,所以这个问题)还是我需要重新考虑我的类层次结构。
PS:如果有一些书可以让我练习这样的设计问题,我也很高兴。我经常遇到这些delimma,不知道该怎么办。
编辑:
类图的使用在clustering
我有成员的另一个类中使用,baseGraph *graph
即
class clustering {
baseGraph *graph;
}
我在这里存储指向基类的指针,以便我可以使用不同的算法(实现为函数)class graph
。对于聚类类,它再次取决于我要使用的图形类型。