Firstly, let us see the wrong version.
class A {
protected:
int price;
};
class C;
class B: public A {
public:
void test(const A& a, const B& b, const C& c);
};
class C: public B {
};
void B::test(const A& a, const B& b, const C& c) {
price=0;
a.price; // bug
b.price;
c.price;
}
int main()
{
A a;
B b;
C c;
b.test(a,b,c);
return 0;
}
We can not visit price through a, which is the object of base class. However, when delete this line, it gets right after all.
本文通过一个简单的C++示例介绍了基类成员在派生类中的可见性问题。演示了如何从派生类中尝试访问基类的受保护成员,并解释了为何这种访问会失败。
872

被折叠的 条评论
为什么被折叠?



