我有一个名为 myGraph
的类,它派生自 graph
如果我在调用构造函数时知道顶点数,我将能够使用下面的构造函数。
myGraph(int numOfV) : graph(numOfV)
{
// initialize...
}
我想计算构造函数中的顶点数,然后继承 graph
inside 构造函数。怎么做?
myGraph(int a, int b)
{
/* using a,b to initialize...
a sequence of push operation on (vector<T>)verticeList */
int numOfV = this->verticeList.size();
// inherit...
myGraph(numOfV); // it will not work
}
请注意
我用来计算顶点数的过程很复杂。(多行代码而不是
a+b
)顶点个数取决于
myGraph
中的实例变量,所以我需要先初始化myGraph
的成员,然后继承剩余的图
部分。
最佳答案
如果预初始化有很多逻辑,您可以在一个方法中将其分开:
struct myGraph
{
myGraph(int a, int b) : graph(myGraph::computeNoVertices(a,b))
{
}
static int computeNoVertices(int a, int b)
{
//whole chunk of code
}
};
关于c++ - 继承构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16953133/