我试图实现调用一个类成员来获取同一类的另一个成员函数的默认参数。这是我正在做的事情:
class y {
virtual vector<int> getLabels();
}
class x: public y {
virtual vector<int> getLabels();
listGraph getPlanarGraph(const vector<int> &nodeSe=getLabels()); //want to achieve this. Compiler won't agree
};
如果没有提供任何内容,即称为obj.getPlanarGraph()
whereobj
是对应类型的,那么我想获取graph
. 我知道我可以为此编写一个简单的包装器,如下所示(见结尾),但我更感兴趣的是为什么不允许这样做。对于上面的声明编译错误是:cannot call member function ‘virtual std::vector<int> baseGraph::getLabels() const’ without object
.
当我提供this
参数时,错误是‘this’ may not be used in this context
.
class x: public y {
virtual vector<int> getLabels();
listGraph getPlanarGraph(const vector<int> &nodeSe=this->getLabels()); //error here.
};
我想到的解决方法是:
class x: public y {
virtual vector<int> getLabels();
listGraph getPlanarGraph(const vector<int> &nodeSet); //No. 2
listGraph getPlanarGraph(); //define the function accordingly and call a 'No. 2' from inside.
};