我正在使用 Visual Studio 2010。
为什么我无法获取指向在子类中“升级”为公共的类方法的指针?
以下代码无法编译:
#include <iostream>
#include <functional>
class Parent {
protected:
void foo() {
std::cout << "Parent::foo()\n";
}
};
class Child : public Parent
{
public:
//void foo() { Parent::foo(); } //This compiles
using Parent::foo; //This does NOT compile
};
int main() {
Child c;
std::function < void () > f = std::bind(&Child::foo, &c);
f();
return 0;
}
它会出现错误:
error C2248: 'Parent::foo' : cannot access protected member declared in class 'Parent'
std::function < void () > f = std::bind(&Child::foo, &c);
时,代码在 TDM-GCC (MINGW) 4.7.1 下编译正常(错误:function
不是 std 的成员)。 - Lorenzo Donati support Ukraine