派生クラスで宣言を使用してstd::bind
「パブリック」にした、プライベート基本クラスからメンバー関数を作成したいと思います。using
関数を直接呼び出すと機能しますが、メンバー関数ポインターをバインドまたは使用するとコンパイルされないようです。
#include <functional>
struct Base {
void foo() { }
};
struct Derived : private Base {
using Base::foo;
};
int main(int, char **)
{
Derived d;
// call member function directly:
// compiles fine
d.foo();
// call function object bound to member function:
// no matching function for call to object of type '__bind<void (Base::*)(), Derived &>'
std::bind(&Derived::foo, d)();
// call via pointer to member function:
// cannot cast 'Derived' to its private base class 'Base'
(d.*(&Derived::foo))();
return 0;
}
上記のエラー メッセージを見ると、問題Derived::foo
はまだ であり、外部からBase::foo
アクセスできないようBase
です。Derived
Derived
これは矛盾しているようです。直接呼び出し、バインドされた関数、および関数ポインターを同じ意味で使用できないようにする必要がありますか?
または(所有していないライブラリにある) を変更せずfoo
に、Derived
オブジェクトにバインドできるようにする回避策はありますか?Base
Derived