分享

C++ 多个相互关联的类的声明方法

 sigmon 2012-03-16

(一)、假如两个类class A和class B,A聚合(组合)B,B又依赖A,那么该怎么来实现呢?

那么可行的方案:

将类A的声明放在A.h,并在类A前加上一句class B;(类的一种声明方法),这样就能在类A中定义类B的指针或引用(但不能实例化和使用类B的成员及方法),类B的声明放在B.h,并在类B前加上一句class A;,这样就能在类B中定义类A的指针或引用;具体定义分别放到A.CPP和B.CPP中,在两个源文件中都包含A.h和B.h,这样各自的方法中就能使用对方类中的成员和方法了。以下是代码示例:

[cpp] 
  1. //A.h  
  2. #ifndef __A_h__  
  3. #define __A_h__  
  4. class B;  
  5. class A  
  6. {  
  7. public:  
  8.     A(void);  
  9.     ~A(void);  
  10.     void funcPrint();  
  11.     void funcCall();  
  12.     B *b;  
  13. };  
  14. #endif  
  15.   
  16. //B.h  
  17. #ifndef __B_h__  
  18. #define __B_h__  
  19. class A;  
  20. class B  
  21. {  
  22. public:  
  23.     void func(A *a);  
  24. };  
  25. #endif  
  26.   
  27. //A.cpp  
  28. #include "A.h"  
  29. #include "B.h"  
  30. #include <iostream>  
  31. A::A(void){b = new B;}  
  32. A::~A(void){delete b;}  
  33. void A::funcPrint(){  
  34.     std::cout <<"test"<<std::endl;  
  35. }  
  36. void A::funcCall(){  
  37.     b->func(this);  
  38. }  
  39.   
  40. //B.cpp  
  41. #include "A.h"  
  42. #include "B.h"  
  43. void B::func(A *a){  
  44.     a->funcPrint();  
  45. }  

 

忌:

假如,A.h中包含了#include "B.h",那么我们不能在B.h中包含#inlcude "A.h",这样会造成包含循环(和重复包含不同,使用了头文件卫士保护也没法避免),编译器将识别不了两个类的声明。

 

(二)、在(一)的的需求再加上,要把类里面方法定义为内联方法(内联类方法,有两种实现方法,但都要求声明和定义放在同一个文件中),那意味着我们不能将类的声明和定义分别放在两个不同的文件中(分开的话,编译器是不允许的),那么该怎么来实现呢?

可行方案①:

将类A和类B的定义和实现都放在一个文件C.h,以下是代码示例:

[cpp]
  1. //C.h  
  2. #include <iostream>  
  3. class B;  
  4. class A  
  5. {  
  6. public:  
  7.     A(void);  
  8.     ~A(void);  
  9.     void funcPrint();  
  10.     void funcCall();  
  11.     B *b;  
  12. };  
  13. class B  
  14. {  
  15. public:  
  16.     void func(A *a);  
  17. };  
  18. A::A(void){b = new B;}  
  19. A::~A(void){delete b;}  
  20. inline void A::funcPrint(){  
  21.     std::cout <<"test"<<std::endl;  
  22. }  
  23. inline void A::funcCall(){  
  24.     b->func(this);  
  25. }  
  26. inline void B::func(A *a){  
  27.     a->funcPrint();  
  28. }  

 

可行方案②:

方案①一般针对类位于同一个层次上。如果类有明显的层次关系,那么可以在一个类的声明中内嵌另一个类的声明(即将一个类作为另一个类的内嵌类)(没有层次关系当然也能这么用),以下是代码示例:

[cpp] 
  1. //C.h  
  2. #include <iostream>  
  3. class A  
  4. {  
  5. public:  
  6.     class B  
  7.     {  
  8.     public:  
  9.         void func(A *a){  
  10.             a->funcPrint();  
  11.         }  
  12.     };  
  13.     A(void){b = new B;}  
  14.     ~A(void){delete b;}  
  15.     void funcPrint(){  
  16.         std::cout <<"test"<<std::endl;  
  17.     }  
  18.     void funcCall(){  
  19.         b->func(this);  
  20.     }  
  21.     B *b;  
  22. };  


下面提供一个代码测试函数,对上面的代码都适用:
[cpp]
  1. //test.cpp  
  2. #include "A.h"  
  3. //#include "C.h"  
  4. #include "stdlib.h"  
  5. int main()  
  6. {  
  7.     A a;  
  8.     a.funcCall();  
  9.     system("pause");  
  10.     return 0;  
  11. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多