友元不能被继承:即,基类的友元未必是子类的友元;某类型的友元的子类未必是此类型的友元。
详细的,可以看以下两节:What does it mean that "friendship isn't inherited, transitive, or reciprocal"?
Just because I grant you friendship access to me doesn't automaticallygrant your kids access to me, doesn't automatically grant your friendsaccess to me, and doesn't automatically grant me access to you.
- I don't necessarily trust the kids of my friends. The privileges offriendship aren't inherited. Derived classes of a friend aren't necessarilyfriends. If class Fred declares that class Base is a friend,classes derived from Base don't have any automatic special access rights toFred objects.
- I don't necessarily trust the friends of my friends. The privilegesof friendship aren't transitive. A friend of a friend isn't necessarily afriend. If class Fred declares class Wilma as a friend, and classWilma declares class Betty as a friend, class Betty doesn'tnecessarily have any special access rights to Fred objects.
- You don't necessarily trust me simply because I declare you myfriend. The privileges of friendship aren't reciprocal. If class Freddeclares that class Wilma is a friend, Wilma objects have special accessto Fred objects but Fred objects do not automatically have special accessto Wilma objects.
What is inherited from the base class?
In principle, a
derived class inherits every member of a
base class except:
- its constructor and its destructor
- its operator=() members
- its friends