C++中的friend class 用法总结

时间:2025-04-22 08:06:48
C++中的友元机制允许类的非公有成员被一个类或者函数访问,友元按类型分为三种:普通非类成员函数作为友元,类的成员函数作为友元,类作为友元。

1 友元的内容

友元包括友元的声明以及友元的定义。友元的声明默认为了extern,就是说友元类或者友元函数的作用域已经扩展到了包含该类定义的作用域,所以即便我们在类的内部定义友元函数也是没有关系的。

2 普通的非成员函数友元

这类友元函数通常是操作符,例如输入输出操作符.示例如下所示:
[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. class OpeClass  
  4. {  
  5.     friend int func(const OpeClass xx);  
  6. public:  
  7.     OpeClass(void);  
  8.     OpeClass(int x,int y);  
  9.     ~OpeClass(void);  
  10. private:  
  11.     int width;  
  12.     int height;  
  13. };  
[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3.   
  4.   
  5. OpeClass::OpeClass(void)  
  6. {  
  7.     width = 50;  
  8.     height = 50;  
  9. }  
  10.   
  11.   
  12. OpeClass::OpeClass(int x,int y):width(x),height(y)  
  13. {  
  14. }  
  15.   
  16.   
  17. OpeClass::~OpeClass(void)  
  18. {  
  19. }  
  20.   
  21.   
  22. int func(const OpeClass xx)  
  23. {  
  24.     return  * ;  
  25. }  


[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6.   
  7. void main()  
  8. {  
  9.     OpeClass XO;  
  10.     cout<<func(XO);  
  11.     system("pause");  
  12. }  

3 类作为友元

类作为友元需要注意的是友元类和原始类之间的相互依赖关系,如果在友元类中定义的函数使用到了原始类的私有变量,那么就需要在友元类定义的文件中包含原始类定义的头文件。
但是在原始类的定义中(包含友元类声明的那个类),就不需要包含友元类的头文件,也不需要在类定义前去声明友元类,因为友元类的声明自身就是一种声明(它指明可以在类外找到友元类),示例程序如下所示:
[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. #include <iostream>  
  4. using namespace std;  
  5. class A  
  6. {  
  7.     friend class B;  
  8. public:  
  9.     ~A(void);  
  10.     static void func()  
  11.     {  
  12.         cout<<"This is in A"<<endl;  
  13.     }  
  14. private:  
  15.     A(){};  
  16.     static const A Test;  
  17. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. const A A::Test = A();  
  4. A::~A(void)  
  5. {  
  6. }  

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. #include ""  
  4. class B  
  5. {  
  6. public:  
  7.     B(void);  
  8.     ~B(void);  
  9.     void func(C& c);  
  10. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4. #include ""  
  5. #include <iostream>  
  6. using namespace std;  
  7.   
  8.   
  9. B::B(void)  
  10. {  
  11. }  
  12.   
  13.   
  14.   
  15.   
  16. B::~B(void)  
  17. {  
  18. }  
  19.   
  20.   
  21. void B::func(C& c)  
  22. {  
  23.     cout<<"This is in B"<<endl;  
  24.     A::();  
  25.     (A::Test);  
  26. }  

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. class A;  
  4. class C  
  5. {  
  6. public:  
  7.     C(void);  
  8.     ~C(void);  
  9.     void func(const A& a);  
  10. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. C::C(void)  
  7. {  
  8. }  
  9.   
  10. C::~C(void)  
  11. {  
  12. }  
  13.   
  14. void C::func(const A& a)  
  15. {  
  16.     cout<<"This is in C"<<endl;  
  17. }  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4. #include ""  
  5. #include <iostream>  
  6. using namespace std;  
  7.   
  8. void main()  
  9. {  
  10.     B b;  
  11.     C c;  
  12.     (c);  
  13.     system("pause");  
  14. }  

4 类成员函数作为友元函数

这个稍微有点复杂,因为你要类成员函数作为友元,你在声明友元的时候要用类限定符,所以必须先定义包含友元函数的类,但是在定义友元的函数时候,又必须事先定义原始类。通常的做法先定义包含友元函数的类,再定义原始类,这个顺序不能乱。(如果是友元类,则没有这种这种必须)如下面所示:

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. class A;  
  4. class B  
  5. {  
  6. public:  
  7.     B(void);  
  8.     ~B(void);  
  9.     int func(A xx);  
  10. };  

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. #include ""  
  4. class A  
  5. {  
  6. friend int B::func(A xx);  
  7. public:  
  8.     A(void):mx(20),my(30){}  
  9.     ~A(void){}  
  10. private:  
  11.     int mx;  
  12.     int my;  
  13. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4.   
  5.   
  6. B::B(void)  
  7. {  
  8. }  
  9.   
  10.   
  11. B::~B(void)  
  12. {  
  13. }  
  14.   
  15. int B::func(A xx)  
  16. {  
  17.     return  * ;  
  18. }  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4. #include <iostream>  
  5. using namespace std;  
  6. void main()  
  7. {  
  8.     A a;  
  9.     B b;  
  10.     cout<<(a)<<endl;  
  11.     system("pause");  
  12. }  

5 友元不具有相互性,只具有单项性 若类B是类A的友元,类A不一定是类B的友元,要看在类中是否有相应的声明。

6 友元不能被继承

B是A的友元类,C是B的子类,推不出C是A的友元

7 友元不具有传递性

B是A的友元,C是B的友元,推不出C是A的友元

8 相互为友元的类

这个其实没什么好注意的,下面是实例,类A,类B互为友元
[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. class A  
  4. {  
  5. friend class B;  
  6. public:  
  7.     A(void);  
  8.     ~A(void);  
  9.     int funa(B& b);  
  10. private:  
  11.     int mx;  
  12.     int my;  
  13. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4.   
  5.   
  6. A::A(void)  
  7. {  
  8.     mx = 10;  
  9.     my = 10;  
  10. }  
  11.   
  12.   
  13. A::~A(void)  
  14. {  
  15. }  
  16.   
  17.   
  18. int A::funa(B& b)  
  19. {  
  20.     return  * ;  
  21. }  

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. class B  
  4. {  
  5.     friend class A;  
  6. public:  
  7.     B(void);  
  8.     ~B(void);  
  9.     int funb(A& a);  
  10. private:  
  11.     int mb;  
  12.     int mc;  
  13. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4.   
  5.   
  6. B::B(void)  
  7. {  
  8.     mb = 20;  
  9.     mc = 20;  
  10. }  
  11.   
  12. B::~B(void)  
  13. {  
  14. }  
  15.   
  16. int B::funb(A& a)  
  17. {  
  18.     return  *;  
  19. }  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4. #include <iostream>  
  5. using namespace std;  
  6. void main()  
  7. {  
  8.     A a;  
  9.     B b;  
  10.     cout<<(b)<<endl;  
  11.     cout<<(a)<<endl;  
  12.     system("pause");  
  13. }  

9 如果想要指定两个类都有成员函数作为对方的友元,那么必须第2个类是第一个类的友元

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3.   
  4. // class B is a friend class of A  
  5. class A  
  6. {  
  7.     friend class B;  
  8. public:  
  9.     A(void):ma(10),mb(20){}  
  10.     ~A(void){}  
  11.     int funa(B& b);  
  12. private:  
  13.     int ma;  
  14.     int mb;  
  15. };  

[cpp]  view plain  copy
  1. //  
  2. #pragma once  
  3. #include ""  
  4.   
  5.   
  6. // A's function funa is a friend function of B  
  7. class B  
  8. {  
  9.     friend int A::funa(B& b);  
  10. public:  
  11.     B(void);  
  12.     ~B(void);  
  13.     int funb(A& a);  
  14.     int func(A& a);  
  15. private:  
  16.     int mx;  
  17.     int my;  
  18. };  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4.   
  5.   
  6. int A::funa(B& b)  
  7. {  
  8.     return   * ;  
  9. }  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3.   
  4. B::B(void):mx(12),my(15)  
  5. {  
  6. }  
  7.   
  8.   
  9. B::~B(void)  
  10. {  
  11. }  
  12.   
  13.   
  14. int B::funb(A& a)  
  15. {  
  16.     return  + ;  
  17. }  
  18.   
  19. int B::func(A& a)  
  20. {  
  21.     return  * ;  
  22. }  

[cpp]  view plain  copy
  1. //  
  2. #include ""  
  3. #include ""  
  4. #include <iostream>  
  5. using namespace std;  
  6. void main()  
  7. {  
  8.     A a;  
  9.     B b;  
  10.     cout<<(b)<<endl;  
  11.     cout<<(a)<<endl;  
  12.     cout<<(a)<<endl;  
  13. }  

转载自/ddupd/article/details/38053159