| Line |   | 
|---|
| 1 | 
 | 
|---|
| 2 | #include <iostream>
 | 
|---|
| 3 | #include "boost/shared_ptr.hpp"
 | 
|---|
| 4 | #include "boost/weak_ptr.hpp"
 | 
|---|
| 5 | 
 | 
|---|
| 6 | class A;
 | 
|---|
| 7 | class B;
 | 
|---|
| 8 | 
 | 
|---|
| 9 | typedef boost::shared_ptr<A> AStrongPtr;
 | 
|---|
| 10 | //typedef boost::weak_ptr<A>   AWeakPtr;
 | 
|---|
| 11 | typedef boost::shared_ptr<B> BStrongPtr;
 | 
|---|
| 12 | //typedef boost::weak_ptr<B>   BWeakPtr;
 | 
|---|
| 13 | 
 | 
|---|
| 14 | class ABlink;
 | 
|---|
| 15 | typedef boost::shared_ptr<ABlink> ABlinkStrongPtr;
 | 
|---|
| 16 | typedef boost::weak_ptr<ABlink> ABlinkWeakPtr;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | 
 | 
|---|
| 19 | class A {
 | 
|---|
| 20 |  public:
 | 
|---|
| 21 |   A() { std::cout << "Ctor A<"<<this<<">" << std::endl;}
 | 
|---|
| 22 |   virtual ~A() { std::cout << "Dtor A<"<<this<<">" << std::endl;}
 | 
|---|
| 23 | };
 | 
|---|
| 24 | 
 | 
|---|
| 25 | class B {
 | 
|---|
| 26 |  public:
 | 
|---|
| 27 |   B() { std::cout << "Ctor B<"<<this<<">" << std::endl;}
 | 
|---|
| 28 |   virtual ~B() { std::cout << "Dtor B<"<<this<<">" << std::endl;}
 | 
|---|
| 29 | };
 | 
|---|
| 30 | 
 | 
|---|
| 31 | class ABlink {
 | 
|---|
| 32 |  public:
 | 
|---|
| 33 | 
 | 
|---|
| 34 |   void setB(const BStrongPtr& aB) {a = aB;}
 | 
|---|
| 35 |   BStrongPtr& getB() { 
 | 
|---|
| 36 |     std::cout << "Use non-const getB" << std::endl;
 | 
|---|
| 37 |     return a;
 | 
|---|
| 38 |   }
 | 
|---|
| 39 |   const BStrongPtr& getB() const { 
 | 
|---|
| 40 |     std::cout << "Use const getB" << std::endl;
 | 
|---|
| 41 |     return b;
 | 
|---|
| 42 |   }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   void setA(const AStrongPtr& aA) {a = aA;}
 | 
|---|
| 45 |   AStrongPtr& getA() { 
 | 
|---|
| 46 |     std::cout << "Use non-const getA" << std::endl;
 | 
|---|
| 47 |     return a;
 | 
|---|
| 48 |   }
 | 
|---|
| 49 |   const AStrongPtr& getA() const { 
 | 
|---|
| 50 |     std::cout << "Use const getA" << std::endl;
 | 
|---|
| 51 |     return a;
 | 
|---|
| 52 |   }
 | 
|---|
| 53 | 
 | 
|---|
| 54 |  private:
 | 
|---|
| 55 |   AStrongPtr a;
 | 
|---|
| 56 |   BStrongPtr b;  
 | 
|---|
| 57 | };
 | 
|---|
| 58 | 
 | 
|---|
| 59 | 
 | 
|---|
| 60 | 
 | 
|---|
| 61 | int main() {
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   std::cout << "Create A & B" << std::endl;
 | 
|---|
| 64 |   AStrongPtr aA(new A);
 | 
|---|
| 65 |   BStrongPtr aB(new B);
 | 
|---|
| 66 |   
 | 
|---|
| 67 |   std::cout << "Make the association A <-> B" << std::endl;
 | 
|---|
| 68 |   ABlinkStrongPtr abLink(new ABlink);
 | 
|---|
| 69 |   abLink->setA(aA);
 | 
|---|
| 70 |   abLink->setB(aB);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   std::cout << "return" << std::endl;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   return 0;
 | 
|---|
| 75 | 
 | 
|---|
| 76 | }
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.