friend declaration in protected section

半腔热情 提交于 2019-12-10 23:32:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!