B has access only to protected fields in itself or to other objects of type B (or, possibly, obtained from B if it sees them as Bs).
B does not have access to the protected fields of any other unrelated objects in the same inheritance tree.
Apple does not have access to the internal components of Orange, even if they are both fruits.
class Fruit
{
protected: int sweetness;
};
class Apple: public Fruit
{
public: Apple() { this->sweetness = 100; }
};
class Orange: public Fruit
{
public:
void evil_function(Fruit& f)
{
f.sweetness = -100;
}
};
int main()
{
Apple apple;
Orange orange;
orange.evil_function(apple);
}
source
share