问题
Is there a meaning to declare friendship in the protected section, rather than in public? For example in this code:
class Shape {
//...
protected:
friend ostream& operator<<(ostream& os, const Shape& s);
virtual void print(ostream& os) const = 0;
};
[Note that Shape is abstract]
Could I have just put the friend and the function declaration in public? Thanks!
回答1:
Is there a meaning to declare friendship in the protected section, rather than in public?
No. The friend class has the same level of access irrespective of whether the friend declaration appears in either the public, protected or private sections of the class definition. link
Could I have just put the friend and the function declaration in public?
thus yes, it doesn't matter whether declaration has been found in private, public or protected part of your class.
来源:https://stackoverflow.com/questions/17907019/friend-declaration-in-protected-section