我想要std::bind
一个来自私有基类的成员函数,using
在派生类中使用 -declaration 将其设为“公共”。直接调用该函数有效,但似乎绑定或使用成员函数指针无法编译:
#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
(在我不拥有的库中)?