【发布时间】:2016-10-13 02:15:25
【问题描述】:
class B
{
protected:
int x;
public:
B(int i=28) { x=i; }
virtual B f(B ob) { return x+ob.x+1; }
void afisare(){ cout<<x; }
};
class D: public B
{
public:
D(int i=-32):B(i) {}
B f(B ob) { return x+ob.x-1; }
};
void test6()
{
B *p1=new D, *p2=new B, *p3=new B(p1->f(*p2));
p3->afisare();
}
main 只是调用函数 test6(); 我的问题是,为什么编译器会在第 3 行抛出错误,在 int x 声明,带有消息:
In member function 'virtual B D::f(B)' :
error: 'int B::x' is protected
error: within this context
PS : 该示例来自考试,因此错误的压痕和其他“泄漏”是故意的。
【问题讨论】:
-
没有理由对 SO 造成奇怪的缩进,是吗?
标签: c++ class oop virtual protected