友元friend总结

1.关键字friend只出现在类定义的内部(这点与const不同)

2.友元是授予友元关系的那个类的成员

3.定义类B为类A的友元,B可以访问A的私有成员

  1. 例1:/*要注意友元的使用顺序,声明B->定义A->定义B*/    
  2. class B;/*只声明而未定义的类称为不完全类,可用定义类型,不能用于定义对象*/    
  3. class A    
  4. {    
  5. private:    
  6.     int x;    
  7. public:    
  8.     A(int i):x(i){}    
  9.     friend B;/*将B声明为友元,B可以访问A的private*/    
  10. };    
  11. class B    
  12. {    
  13. private:    
  14.     int x;    
  15. public:    
  16.     void Test(A a)    
  17.     {    
  18.         x = a.x;    
  19.     }    
  20.     int Get(){return x;}    
  21. };    
  22. int main()    
  23. {    
  24.     A a(3);    
  25.     B b;    
  26.     b.Test(a);    
  27.     //输出3.说明Test()函数赋值成功,也说明Test()中的x是B的成员,从而得知,Test()是属于B的      
  28.     cout<<b.Get()<<endl;    
  29.     return 0;    
  30. }    
例1:/*要注意友元的使用顺序,声明B->定义A->定义B*/  
class B;/*只声明而未定义的类称为不完全类,可用定义类型,不能用于定义对象*/  
class A  
{  
private:  
    int x;  
public:  
    A(int i):x(i){}  
    friend B;/*将B声明为友元,B可以访问A的private*/  
};  
class B  
{  
private:  
    int x;  
public:  
    void Test(A a)  
    {  
        x = a.x;  
    }  
    int Get(){return x;}  
};  
int main()  
{  
    A a(3);  
    B b;  
    b.Test(a);  
    //输出3.说明Test()函数赋值成功,也说明Test()中的x是B的成员,从而得知,Test()是属于B的   
    cout<<b.Get()<<endl;  
    return 0;  
}  


4.定义类B的Test()函数为类A的友元,B的Test()函数可以访问A的私有成员

  1. 例2:/*要注意友元的使用顺序, 
  2. 声明A->定义B(要作为友元的那个函数只声明不定义)->定义A->定义B的函数*/  
  3. class A;/*只声明而未定义的类称为不完全类,可用定义类型,不能用于定义对象*/  
  4. class B  
  5. {  
  6. private:  
  7.     int x;  
  8. public:  
  9.     void Test(A a);  
  10.     int Get(){return x;}  
  11. };  
  12. class A  
  13. {  
  14. private:  
  15.     int x;  
  16. public:  
  17.     A(int i):x(i){}  
  18.     friend void B::Test(A a);/*将B的Test()声明为友元,B的Test()可以访问A的private*/  
  19. };  
  20. void B::Test(A a){x = a.x;}  
  21. int main()  
  22. {  
  23.     A a(3);  
  24.     B b;  
  25.     b.Test(a);  
  26.     //输出3.说明Test()函数赋值成功,也说明Test()中的x是B的成员,从而得知,Test()是属于B的   
  27.     cout<<b.Get()<<endl;  
  28.     return 0;  
  29. }  
例2:/*要注意友元的使用顺序,
声明A->定义B(要作为友元的那个函数只声明不定义)->定义A->定义B的函数*/
class A;/*只声明而未定义的类称为不完全类,可用定义类型,不能用于定义对象*/
class B
{
private:
	int x;
public:
	void Test(A a);
	int Get(){return x;}
};
class A
{
private:
	int x;
public:
	A(int i):x(i){}
	friend void B::Test(A a);/*将B的Test()声明为友元,B的Test()可以访问A的private*/
};
void B::Test(A a){x = a.x;}
int main()
{
	A a(3);
	B b;
	b.Test(a);
	//输出3.说明Test()函数赋值成功,也说明Test()中的x是B的成员,从而得知,Test()是属于B的
	cout<<b.Get()<<endl;
	return 0;
}


5.友元声明将已命名的类或非成员函数引入到外围作用域中。此外,友元函数可以在类的内部定义,该函数的作用域扩展到包围该类定义的作用域。

例3:

  1. /*本来A和f()的作用域只限于在它们声明之后。 
  2. 但是因为类B提前引入了它们,使得它们的作用域与类B的作用域相同*/  
  3. class B  
  4. {  
  5.     friend class A;  
  6.     friend void f();  
  7. };  
  8. class C  
  9. {  
  10. public:  
  11.     A *p;  
  12.     void g(){return f();}  
  13. };  
  14. class A;  
  15. void f()  
  16. {  
  17.     cout<<"1"<<endl;  
  18. }  
  19. int main()  
  20. {  
  21.     C c;  
  22.     c.g();//输入1   
  23.     return 0;  
  24. }  
/*本来A和f()的作用域只限于在它们声明之后。
但是因为类B提前引入了它们,使得它们的作用域与类B的作用域相同*/
class B
{
	friend class A;
	friend void f();
};
class C
{
public:
	A *p;
	void g(){return f();}
};
class A;
void f()
{
	cout<<"1"<<endl;
}
int main()
{
	C c;
	c.g();//输入1
	return 0;
}


6.友元函数的重载函数,如果要成为友元,必须要用friend声明。

例4:

  1. class A;/*只声明而未定义的类称为不完全类,可用定义类型,不能用于定义对象*/  
  2. class B  
  3. {  
  4. private:  
  5.     int x;  
  6. public:  
  7.     void Test(A a);  
  8.     void Test(A a1, A a2);  
  9.     void Test(A a1, A a2, A a3);  
  10.     int Get(){return x;}  
  11. };  
  12. class A  
  13. {  
  14. private:  
  15.     int x;  
  16. public:  
  17.     A(int i):x(i){}  
  18.     friend void B::Test(A a);/*将B的Test()声明为友元,B的Test()可以访问A的private*/  
  19.     friend void B::Test(A a1, A a2);  
  20. };  
  21. //正确,因为已经被定义为友元   
  22. void B::Test(A a){x = a.x;}  
  23. //正确,因为已经被定义为友元   
  24. void B::Test(A a1, A a2){x = a1.x + a2.x;}  
  25. //错误,不能因为Test(A a)是友元,就认为它的重载也是友元   
  26. void B::Test(A a1, A a2, A a3){x = a1.x + a2.x + a3.x;}  
class A;/*只声明而未定义的类称为不完全类,可用定义类型,不能用于定义对象*/
class B
{
private:
	int x;
public:
	void Test(A a);
	void Test(A a1, A a2);
	void Test(A a1, A a2, A a3);
	int Get(){return x;}
};
class A
{
private:
	int x;
public:
	A(int i):x(i){}
	friend void B::Test(A a);/*将B的Test()声明为友元,B的Test()可以访问A的private*/
	friend void B::Test(A a1, A a2);
};
//正确,因为已经被定义为友元
void B::Test(A a){x = a.x;}
//正确,因为已经被定义为友元
void B::Test(A a1, A a2){x = a1.x + a2.x;}
//错误,不能因为Test(A a)是友元,就认为它的重载也是友元
void B::Test(A a1, A a2, A a3){x = a1.x + a2.x + a3.x;}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值