Reputation: 2033
According to the text I am referring to (The Complete Reference by Herbert Schildt) a derived class doesn't inherit friend functions and friend functions may not have a storage-class specifier. That is, they may not be declared as static or extern. Why?
Upvotes: 5
Views: 964
Reputation: 76306
static
nor extern
, because it is not part of the function signature, so it's not needed to specify the function. I believe the declaration of the function itself can (outside of the class) contain either.Upvotes: 7
Reputation: 545696
a derived class doesn't inherit friend functions? […] why?
Because that would break encapsulation: the derived class couldn’t control its friends any longer so it effectively cannot control who has access to its internals.
they may not be declared as static or extern, why?
Because static
would make no sense (it only makes sense in functions belonging to a class, and friend
s are free functions), and extern
would once again violate encapsulation because the class effectively cannot control any longer which function has access to it: due to being extern
, the friend could effectively come from a different compilation unit, unknown to the class.
See Jan’s answer for a correction.
Upvotes: 9