C++中的friend class 用法总结 对于一个没有定义public访问权限的类,能够让其他的类操作它的私有成员往往是有用的。例如你写了一段binary tree的代码,Node是节点类,如果能够让连接多个节点的函数不需要调用public方法就能够访问到Node的私有成员的话,一定是很方便的。

对于一个没有定义public访问权限的类,能够让其他的类操作它的私有成员往往是有用的。例如你写了一段binary tree的代码,Node是节点类,如果能够让连接多个节点的函数不需要调用public方法就能够访问到Node的私有成员的话,一定是很方便的。

Friend Classes(友元类)

C++中的friend关键字其实做这样的事情:在一个类中指明其他的类(或者)函数能够直接访问该类中的private和protected成员。

你可以这样来指明:

friend class aClass;
注意:friend在类中的声明可以再public、protected和private的如何一个控制域中,而不影响其效果。例如,如果你在protected域中有这样的声明,那么aClass类同样可以访问该类的private成员。

这有一个例子:

[html]  view plain  copy
 
  1. class Node   
  2. {  
  3.     private:   
  4.        int data;  
  5.        int key;  
  6.        // ...  
  7.   
  8.     friend class BinaryTree; // class BinaryTree can now access data directly  
  9. };  
这样BinaryTree就可以直接访问Node中的private的成员了,就像下面这样:
[html]  view plain  copy
 
  1. class BinaryTree  
  2. {  
  3.     private:  
  4.        Node *root;  
  5.   
  6.     int find(int key);  
  7. };  
  8. int BinaryTree::find(int key)  
  9. {  
  10.     // check root for NULL...  
  11.     if(root->key == key)  
  12.     {  
  13.         // no need to go through an accessor function  
  14.         return root->data;  
  15.     }  
  16.     // perform rest of find  
  17. }  

Friend Functions(友元函数)

友元函数和友元类的作用是一样的,它允许一个函数不需要通过其public接口就能够访问到类中的private和protected成员变量。

你可以这样去声明:

friend return_type class_name::function(args);
这里有一个例子:
class Node 
{
    private: 
       int data;
       int key;
       // ...

    friend int BinaryTree::find(); // Only BinaryTree's find function has access
};
这样find方法就可以直接访问Node中的私有成员变量了,而BinaryTree中的其他的方法去不能够直接的访问Node的成员变量。(这就是友元类和友元函数的区别)

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

1 友元的内容

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

2 普通的非成员函数友元

这类友元函数通常是操作符,例如输入输出操作符.示例如下所示:
[cpp]  view plain  copy
  1. //OpeClass.h  
  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. //OpeClass.cpp  
  2. #include "OpeClass.h"  
  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 xx.height * xx.width;  
  25. }  


[cpp]  view plain  copy
  1. //main.cpp  
  2. #include "OpeClass.h"  
  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. //A.h  
  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. //A.cpp  
  2. #include "A.h"  
  3. const A A::Test = A();  
  4. A::~A(void)  
  5. {  
  6. }  

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

[cpp]  view plain  copy
  1. //B.cpp  
  2. #include "B.h"  
  3. #include "A.h"  
  4. #include "C.h"  
  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::Test.func();  
  25.     c.func(A::Test);  
  26. }  

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

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

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

[cpp]  view plain  copy
  1. //B.h  
  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. //A.h  
  2. #pragma once  
  3. #include "B.h"  
  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. //B.cpp  
  2. #include "B.h"  
  3. #include "A.h"  
  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 xx.mx * xx.my;  
  18. }  

[cpp]  view plain  copy
  1. //main.cpp  
  2. #include "A.h"  
  3. #include "B.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6. void main()  
  7. {  
  8.     A a;  
  9.     B b;  
  10.     cout<<b.func(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. //A.h  
  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. //A.cpp  
  2. #include "A.h"  
  3. #include "B.h"  
  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 b.mb * b.mc;  
  21. }  

[cpp]  view plain  copy
  1. //B.h  
  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. //B.cpp  
  2. #include "B.h"  
  3. #include "A.h"  
  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 a.mx *a.my;  
  19. }  

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

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

[cpp]  view plain  copy
  1. //A.h  
  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. //B.h  
  2. #pragma once  
  3. #include "A.h"  
  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. //A.cpp  
  2. #include "A.h"  
  3. #include "B.h"  
  4.   
  5.   
  6. int A::funa(B& b)  
  7. {  
  8.     return  b.mx * b.my;  
  9. }  

[cpp]  view plain  copy
  1. //B.cpp  
  2. #include "B.h"  
  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 a.ma + a.mb;  
  17. }  
  18.   
  19. int B::func(A& a)  
  20. {  
  21.     return a.ma * a.mb;  
  22. }  

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

转载自https://blog.csdn.net/ddupd/article/details/38053159
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuhuangtianzi/article/details/38053159

猜你喜欢

转载自blog.csdn.net/weixin_38293850/article/details/80191242