クラスメンバーを呼び出して、同じクラスの別のメンバー関数のデフォルトパラメーターを取得しようとしています。これが私がやっていることです:
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.
};