[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
友元机制允许一个类将对其非公有制成员的访问权授予指定的函数或类。一般友元声明以关键字friend开始。
友元只能出现在类定义的内部。而友元的声明可以出现在类中的任何地方。一般友元声明被成批地放在类定义的开始或结尾。最简单的使用友元机制的例子如下:
class Screen { friend class Windows ; ...... }
在上例中,类Windows被声明为类Screen的友元类,之后在类Windows中可以使用类Screen的非公有资源,包括其私有成员。友元可以是普通非成员函数,其他类的成员函数或整个类。
在ISO/IEC
14882:2003(E)中,类或类模板的友元可以是函数模板或者类模板。友元模板可以在类(或类模板)内部被声明。友元函数可以在类(或类模板)中定义,而友元类模板不可以在类(或类模板)中被定义。
类模板中友元定义主要分为以下3类:
* 非模板函数、类作为实例类的友元;
* 模板函数、模板类作为同类型实例类的友元;
* 模板函数、类作为不同类型实例类的友元。
下面分别举例说明。
(1) 非模板函数、类作为实例类的友元。举例如下:
class A{ void AF(); }; template <class Type> class Queue { friend class B;
//类B不需要事先声明 friend void A(); //函数A() friend void A::AF(); //类A必须事先声明 }
(2 )模板函数、模板类作为同类型实例类的友元。举例如下:
template <class Type> class A{...}; template <class Type> void D() (B <Type>);
template <class Type> class C{ void Cf();...}; template <class Type>class B {
friend class A<Type>; //模板类A需要先定义或声明 friend void D(B <Type>); //模板函数A()需要先定义或声明
friend void C<Type>::Cf(); //模板类C必须先定义 };
(3)模板函数、类作为不同类型实例类的友元。举例如下:
template <class T>class B { friend<class Type>friend class A; template <class
Type>friend void D(B <Type>); template <class Type>friend void C::Cf(); }