- // class A
- #include <boost/shared_ptr.hpp>
- #include "B.h"
- using namespace boost;
- class B;
- class A
- {
- public:
- A()
- {
- mB = NULL;
- mI = 1;
- }
- ~A()
- {
- if( NULL!=mB )
- {
- delete mB;
- }
- }
- void test1()
- {
- shared_ptr <A> ptr(this);
- B b(ptr);
- b.print();
- }
- };
- //class B
- #include <boost/shared_ptr.hpp>
- #include <iostream>
- using namespace boost;
- using namespace std;
- class A;
- class B
- {
- public:
- B(shared_ptr <A> a)
- {
- A* a_ptr=a.get();
- mAshptr = a;
- }
- B(A* a)
- {
- mAptr = a;
- }
- B(A& a){mAptr=&a;}
- ~B()
- {
- }
- void print()
- {
- cout < <"BB" < <endl;
- }
- private:
- shared_ptr <A> mAshptr;
- A* mAptr;
- };
- //main.cpp
- #include "A.h"
- int main()
- {
- shared_ptr <A> ptr(new A());
- ptr->test1();
- }
- #include <boost/shared_ptr.hpp>
- #include <boost/enable_shared_from_this.hpp>
- #include "B.h"
- using namespace boost;
- class B;
- class A : public enable_shared_from_this <A>
- {
- public:
- A()
- {
- mB = NULL;
- mI = 1;
- }
- ~A()
- {
- if( NULL!=mB )
- {
- delete mB;
- }
- }
- void test1()
- {
- shared_ptr <A> ptr = shared_from_this();
- B b(ptr);
- b.print();
- }
- };