C++:关于template与friend联合出现的问题

1(C++ primer 5th edition 7.2.1: Declarations for Friends)

A friend declaraton only specifies access. It is not a general declaration of the function.//

If we want users of the class to be able to call a friend function, then we have to make a friend visible to users of the class, the usual method is  to declare each friend in the same header as the class itsself, outside the class.

也就是说,在一个class的definition body中说一个函数是这个class的friend,起到的作用只是赋予这个函数能够使用class的nonpublic成员的权限。

而如果我们需要用户使用这个函数的话,我们需要告诉compiler这个函数的原型(给出所有调用这个函数需要的information),就要做出一般的声明,这就是一般的function declaration。


2(C++ primer 5th edition 7.2)

关于friend的另几个概念:

一:"Friend eclarations may appear only(注意only) inside a class definition",进一步的是“they may appear anywhere in the class.”这是关于friend出现位置的要求。

二:"Friends are not members of the class and are not affected by the access control of the section in which they are declared." 也就是说,friend虽然在class的definition中出现,但是它却不是class的一个member,并且也不受private,public这种权限的控制。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值