使用enable_shared_from_this示例

 1 /*测试enable_shared_from_this*/

 2 #include <iostream>

 3 #include <boost/smart_ptr/shared_ptr.hpp>

 4 #include <boost/smart_ptr/weak_ptr.hpp>

 5 #include <boost/smart_ptr/enable_shared_from_this.hpp>

 6 using namespace std;

 7 

 8 class TestClass : public enable_shared_from_this<TestClass>

 9 {

10 public:

11 private:

12     int var;

13 };

14 

15 void main()

16 {

17     TestClass *pobj = new TestClass();

18 

19     shared_ptr<TestClass> sptr(pobj);

20     if (sptr==pobj->shared_from_this())

21     {

22         cout << "==" << endl;

23     }

24     else

25     {

26         cout << "!=" << endl;

27     }

28 

29     shared_ptr<TestClass> sptr2;

30     if (sptr2==pobj->shared_from_this())

31     {

32         cout << "==" << endl;

33     }

34     else

35     {

36         cout << "!=" << endl;

37     }

38 }

 

你可能感兴趣的:(this)