friend declaration in protected section

2019-07-12 14:00发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-07-12 14:33

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.

查看更多
登录 后发表回答