赞助

C++中两个类中互相包含对方对象的指针问题(转载)

出处:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html

 

 1 // A.h
 2 #include "B.h"
 3 class A
 4 {
 5 public:
 6     A(B* pB):m_pB(pB) {}
 7     void useB();
 8 private:
 9     B* m_pB;
10 };
11 
12 //A.cpp
13 #include "A.h"
14 void A::useB()
15 {
16     //do something for m_pB
17 }
18 
19 
20 //B.h
21 class A;
22 class B
23 {
24 public:
25     B(A* pA):m_pA(pA) {}
26     void useA();
27     
28 private:
29     A* m_pA;
30 };
31 
32 //B.h
33 #include "A.h"
34 #include "B.h"
35 void B::useA()
36 {
37     //do something for m_pA
38     m_pA = new A;
39 }

 

posted @ 2015-02-10 15:24  车臣  阅读(210)  评论(0编辑  收藏  举报