关于智能指针和拷贝构造函数的一个例子

[复制链接]
 楼主| keer_zu 发表于 2022-10-9 16:31 | 显示全部楼层 |阅读模式
代码:
  1. #include <iostream>
  2. #include <memory>

  3. using namespace std;

  4. class B;    //声明
  5. class A
  6. {
  7. public:
  8.     shared_ptr<B> pb_;
  9.     ~A()
  10.     {
  11.         cout << "A delete\n";
  12.     }
  13. };

  14. class B
  15. {
  16. public:
  17.     shared_ptr<A> pa_;
  18.     ~B()
  19.     {
  20.         cout << "B delete\n";
  21.     }
  22. };

  23. void test_w()
  24. {
  25.     shared_ptr<B> pb(new B());
  26.     shared_ptr<A> pa(new A());
  27.     cout << pb.use_count() << endl;    //1
  28.     cout << pa.use_count() << endl;    //1
  29.     pb->pa_ = pa;
  30.     pa->pb_ = pb;
  31.     cout << pb.use_count() << endl;    //2
  32.     cout << pa.use_count() << endl;    //2
  33. }



  34. void test_shared()
  35. {
  36.     string *s1 = new string("s1");

  37.     shared_ptr<string> ps1(s1);
  38.     shared_ptr<string> ps2;
  39.     cout << ps1.unique() << endl;
  40.     cout << ps2.unique() << endl;
  41.     ps2 = ps1;

  42.     cout << ps1.use_count()<<endl;    //2
  43.     cout<<ps2.use_count()<<endl;    //2
  44.     cout << ps1.unique()<<endl;    //0

  45.     string *s3 = new string("s3");
  46.     shared_ptr<string> ps3(s3);

  47.     cout << (ps1.get()) << endl;    //033AEB48
  48.     cout << ps3.get() << endl;    //033B2C50
  49.     cout << ps1.use_count() << endl;
  50.     cout << ps3.use_count() << endl;
  51.     cout << __LINE__ << "_ps1:" << *ps1 << endl;
  52.     cout << __LINE__ << "_ps3:" << *ps3 << endl;
  53.     cout << __LINE__ << "_ps2:" << *ps2 << endl;
  54.     swap(ps1, ps3);    //交换所拥有的对象
  55.     cout << (ps1.get())<<endl;    //033B2C50
  56.     cout << ps3.get() << endl;    //033AEB48
  57.     cout << __LINE__ << "_ps1:" << *ps1 << endl;
  58.     cout << __LINE__ << "_ps3:" << *ps3 << endl;
  59.     cout << ps1.use_count()<<endl;    //1
  60.     cout << ps2.use_count() << endl;    //2
  61.     ps2 = ps1;
  62.     cout << __LINE__ << "_ps2:" << *ps2 << endl;
  63.     cout << __LINE__ << "_ps1:" << *ps1 << endl;
  64.     cout << ps1.use_count()<<endl;    //2
  65.     cout << ps2.use_count() << endl;    //2
  66.     ps1.reset();    //放弃ps1的拥有权,引用计数的减少
  67.     cout << ps1.use_count()<<endl;    //0
  68.     cout << ps2.use_count()<<endl;    //1
  69. }


  70. int test2()
  71. {
  72.     //初始化方式1
  73.     std::unique_ptr<int> up1(new int(123));
  74.     //初始化方式2
  75.     std::unique_ptr<int> up2;
  76.     up2.reset(new int(456));
  77.     //初始化方式3 (-std=c++14)
  78.     std::unique_ptr<int> up3 = std::make_unique<int>(789);
  79.     cout << *up1 << " " << *up2 << " " << *up3 << endl;
  80. }

  81. class Complex
  82. {
  83. public:
  84.         ~Complex(){cout << "_Destory_" << __LINE__ << endl;}

  85.         Complex(int,int){cout << "_Creat_" << __LINE__ << endl;}
  86.         Complex(const Complex&){cout << "_Creat_cp_" << __LINE__ << endl;}
  87. };
  88. void Fun(Complex c1)
  89. {
  90. }
  91. Complex Fun2()
  92. {
  93.   Complex c(10,20);
  94.   return c;
  95. }

  96. void test3()
  97. {
  98.         cout << "c1:" << endl;
  99.   Complex c1(1,2);
  100.   cout << "Fun(c1):" << endl;
  101.   Fun(c1);
  102.   cout << "c2:" << endl;
  103.   Complex c2 = Fun2();
  104.   cout << "c3:" << endl;
  105.   Complex &c3 = c2;
  106. }

  107. int main()
  108. {
  109.         test3();
  110.         test2();
  111.         test_w();
  112.         //test_shared();
  113.         /*
  114.         unique_ptr<string> ps1, ps2;
  115.         ps1 = unique_ptr<string>(new string ("Hello"));
  116.         ps2 = move(ps1);
  117.         ps1 = unique_ptr<string>(new string ("nihao"));
  118.         cout << *ps2 << " " << *ps1 << endl;
  119.         */
  120.     return 0;
  121. }

 楼主| keer_zu 发表于 2022-10-9 16:32 | 显示全部楼层
运行结果:


  1. zukeqiang@35fb9925ca21:~/test$ ./oz
  2. c1:
  3. _Creat_100
  4. Fun(c1):
  5. _Creat_cp_101
  6. _Destory_98
  7. c2:
  8. _Creat_100
  9. c3:
  10. _Destory_98
  11. _Destory_98
  12. 123 456 789
  13. 1
  14. 1
  15. 2
  16. 2
 楼主| keer_zu 发表于 2022-10-9 16:44 | 显示全部楼层
例子做了修改:

  1. #include <iostream>
  2. #include <memory>

  3. using namespace std;

  4. class B;    //声明
  5. class A
  6. {
  7. public:
  8.     shared_ptr<B> pb_;
  9.     ~A()
  10.     {
  11.         cout << "A delete\n";
  12.     }
  13. };

  14. class B
  15. {
  16. public:
  17.     shared_ptr<A> pa_;
  18.     ~B()
  19.     {
  20.         cout << "B delete\n";
  21.     }
  22. };

  23. void test_w()
  24. {
  25.     shared_ptr<B> pb(new B());
  26.     shared_ptr<A> pa(new A());
  27.     cout << pb.use_count() << endl;    //1
  28.     cout << pa.use_count() << endl;    //1
  29.     pb->pa_ = pa;
  30.     pa->pb_ = pb;
  31.     cout << pb.use_count() << endl;    //2
  32.     cout << pa.use_count() << endl;    //2
  33. }



  34. void test_shared()
  35. {
  36.     string *s1 = new string("s1");

  37.     shared_ptr<string> ps1(s1);
  38.     shared_ptr<string> ps2;
  39.     cout << ps1.unique() << endl;
  40.     cout << ps2.unique() << endl;
  41.     ps2 = ps1;

  42.     cout << ps1.use_count()<<endl;    //2
  43.     cout<<ps2.use_count()<<endl;    //2
  44.     cout << ps1.unique()<<endl;    //0

  45.     string *s3 = new string("s3");
  46.     shared_ptr<string> ps3(s3);

  47.     cout << (ps1.get()) << endl;    //033AEB48
  48.     cout << ps3.get() << endl;    //033B2C50
  49.     cout << ps1.use_count() << endl;
  50.     cout << ps3.use_count() << endl;
  51.     cout << __LINE__ << "_ps1:" << *ps1 << endl;
  52.     cout << __LINE__ << "_ps3:" << *ps3 << endl;
  53.     cout << __LINE__ << "_ps2:" << *ps2 << endl;
  54.     swap(ps1, ps3);    //交换所拥有的对象
  55.     cout << (ps1.get())<<endl;    //033B2C50
  56.     cout << ps3.get() << endl;    //033AEB48
  57.     cout << __LINE__ << "_ps1:" << *ps1 << endl;
  58.     cout << __LINE__ << "_ps3:" << *ps3 << endl;
  59.     cout << ps1.use_count()<<endl;    //1
  60.     cout << ps2.use_count() << endl;    //2
  61.     ps2 = ps1;
  62.     cout << __LINE__ << "_ps2:" << *ps2 << endl;
  63.     cout << __LINE__ << "_ps1:" << *ps1 << endl;
  64.     cout << ps1.use_count()<<endl;    //2
  65.     cout << ps2.use_count() << endl;    //2
  66.     ps1.reset();    //放弃ps1的拥有权,引用计数的减少
  67.     cout << ps1.use_count()<<endl;    //0
  68.     cout << ps2.use_count()<<endl;    //1
  69. }


  70. int test2()
  71. {
  72.     //初始化方式1
  73.     std::unique_ptr<int> up1(new int(123));
  74.     //初始化方式2
  75.     std::unique_ptr<int> up2;
  76.     up2.reset(new int(456));
  77.     //初始化方式3 (-std=c++14)
  78.     std::unique_ptr<int> up3 = std::make_unique<int>(789);
  79.     cout << *up1 << " " << *up2 << " " << *up3 << endl;
  80. }

  81. class Complex
  82. {
  83. private:
  84.     int Real;
  85.     int Image;

  86. public:
  87.         ~Complex(){cout << "_Destory_" << __LINE__ << endl;}

  88.         Complex(int r,int i):Real(r),Image(i){cout << "_Creat_" << __LINE__ << endl;}
  89.         Complex(const Complex& c):Real(c.Real),Image(c.Image){cout << "_Creat_cp_" << __LINE__ << endl;}
  90. };
  91. void Fun(Complex c1)
  92. {
  93. }
  94. Complex Fun2()
  95. {
  96.   Complex c(10,20);
  97.   return c;
  98. }

  99. void test3()
  100. {
  101.         cout << "c1:" << endl;
  102.   Complex c1(1,2);
  103.   cout << "Fun(c1):" << endl;
  104.   Fun(c1);
  105.   cout << "c2:" << endl;
  106.   Complex c2 = Fun2();
  107.   cout << "c3:" << endl;
  108.   Complex &c3 = c2;
  109.   cout << "c4:" << endl;
  110.   Complex c4 = c3;
  111.   cout << "c5:" << endl;
  112.   Complex c5(c4);
  113.   cout << "c5 value:"  << endl;
  114. }

  115. int main()
  116. {
  117.         test3();
  118.         test2();
  119.         test_w();
  120.         //test_shared();
  121.         /*
  122.         unique_ptr<string> ps1, ps2;
  123.         ps1 = unique_ptr<string>(new string ("Hello"));
  124.         ps2 = move(ps1);
  125.         ps1 = unique_ptr<string>(new string ("nihao"));
  126.         cout << *ps2 << " " << *ps1 << endl;
  127.         */
  128.     return 0;
  129. }

 楼主| keer_zu 发表于 2022-10-9 16:45 | 显示全部楼层
结果:


  1. zukeqiang@35fb9925ca21:~/test$ ./oz
  2. c1:
  3. _Creat_104
  4. Fun(c1):
  5. _Creat_cp_105
  6. _Destory_102
  7. c2:
  8. _Creat_104
  9. c3:
  10. c4:
  11. _Creat_cp_105
  12. c5:
  13. _Creat_cp_105
  14. c5 value:
  15. _Destory_102
  16. _Destory_102
  17. _Destory_102
  18. _Destory_102
  19. 123 456 789
  20. 1
  21. 1
  22. 2
  23. 2
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1474

主题

12900

帖子

55

粉丝
快速回复 返回顶部 返回列表