在保护部分的朋友声明 [英] friend declaration in protected section
问题描述
有没有意义在受保护的部分,而不是在公开宣布友谊?
例如在此代码中:
类Shape {
// ...
protected:
friend ostream& operator<<(ostream& os,const Shape& s);
virtual void print(ostream& os)const = 0;
};
[注意Shape是抽象的] b
我可以把朋友和函数声明公开吗?
谢谢!
在受保护部分中声明友谊有意义,
而不是公开?
否。不管朋友声明是出现在类定义的公共,保护还是私有部分中,朋友类都具有相同的访问级别。
链接
我可以把朋友和函数声明放在
public?
无论是否在您的班级的私人,公共或受保护的部分找到了声明。
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!
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.
这篇关于在保护部分的朋友声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!