include <stdio.h>
class Base
{
protected:
int foo;
int get_foo() { return foo; }
};
class Derived : public Base
{
public:
void bar()
{
int Base::* i = &Base::foo;
this->*i = 7;
printf("foo is %d\n", get_foo());
}
};
int main()
{
Derived d;
d.bar();
}
我不明白为什么我的派生类型不能指向基类的受保护成员。它有权访问该成员。它可以调用类似范围的函数。为什么它不能做一个成员指针?我正在使用 gcc 4.1.2 并收到此错误:
test.cc: In member function ‘void Derived::bar()’:
test.cc:6: error: ‘int Base::foo’ is protected
test.cc:15: error: within this context