このようなクラスを持つ:
class A {
public:
bool hasGrandChild() const;
private:
bool hasChild() const;
vector<A> children_;
};
hasChild()
このようなメソッドで定義されたラムダ式でプライベート メソッドを使用できないのはなぜhasGrandChild()
ですか?
bool A::hasGrandChild() const {
return any_of(children_.begin(), children_.end(), [](A const &a) {
return a.hasChild();
});
}
コンパイラは、メソッドhasChild()
がコンテキスト内でプライベートであるというエラーを発行します。回避策はありますか?
編集: 私が投稿したコードは元々機能しているようです。同等だと思っていたのですが、GCCで動かないコードは以下のようなものです。
#include <vector>
#include <algorithm>
class Foo;
class BaseA {
protected:
bool hasChild() const { return !children_.empty(); }
std::vector<Foo> children_;
};
class BaseB {
protected:
bool hasChild() const { return false; }
};
class Foo : public BaseA, public BaseB {
public:
bool hasGrandChild() const {
return std::any_of(children_.begin(), children_.end(), [](Foo const &foo) {
return foo.BaseA::hasChild();
});
}
};
int main()
{
Foo foo;
foo.hasGrandChild();
return 0;
}
これは機能しないため、完全修飾名に問題があるようですが、これは機能します。