当前位置:   article > 正文

关于C++1X中智能指针std::shared_ptr的使用示例

关于C++1X中智能指针std::shared_ptr的使用示例
吐槽一下,现在CSDN的编辑器真实太大妈烂!
  1. 本实例演示了
  2. std::shared_ptr<T>的初始化的集中方法,
  3. 使用上不能使用栈上对象初始化
  4. 删除器的使用
  5. 函数传参的使用
  6. 等使用方法
  7. // smart-ptr.cpp: 定义控制台应用程序的入口点。
  8. //
  9. #include "stdafx.h"
  10. #include <iostream>
  11. #include <memory>
  12. using namespace std;
  13. class cls
  14. {
  15. public:
  16.     cls()
  17.     {
  18.         printf("cls(), this=%x\r\n", this);
  19.     }
  20.     ~cls()
  21.     {
  22.         printf("~cls(), this=%x\r\n", this);
  23.     }
  24.     cls(const cls & a)
  25.     {
  26.         printf("cls(const cls & a), this=%x\r\n",this);
  27.     }
  28.     cls(cls && a)
  29.     {
  30.         printf("cls(cls && a), this=%x\r\n", this);
  31.     }
  32.     cls& operator=(const cls & a)
  33.     {
  34.         printf("cls& operator=(const cls &a), this=%x\r\n", this);
  35.     }
  36.     cls& operator=(const cls && a)
  37.     {
  38.         printf("cls& operator=(const cls && a), this=%x\r\n", this);
  39.     }
  40. };
  41. void func_delete(cls *x)
  42. {
  43.     printf("in function deleter\r\n");
  44.     delete x;
  45. }
  46. class functor
  47. {
  48. public:
  49.     functor()
  50.     {
  51.         printf("functor(), this=%x\r\n", this);
  52.     }
  53.     ~functor()
  54.     {
  55.         printf("~functor(), this=%x\r\n", this);
  56.     }
  57.     functor(const functor & a)
  58.     {
  59.         printf("functor(const functor & a), this=%x\r\n", this);
  60.     }
  61.     functor(functor && a)
  62.     {
  63.         printf("functor(functor && a), this=%x\r\n", this);
  64.     }
  65.     functor& operator=(const functor & a)
  66.     {
  67.         printf("functor& operator=(const functor &a), this=%x\r\n", this);
  68.     }
  69.     functor& operator=(const functor && a)
  70.     {
  71.         printf("functor& operator=(const functor && a), this=%x\r\n", this);
  72.     }
  73.     void operator()(cls *x)
  74.     {
  75.         printf("in functor deleter, this=%x\r\n", this);
  76.         delete x;
  77.     }
  78. };
  79. void func(std::shared_ptr<cls> *ptra)
  80. {
  81.     printf("ptra.use_count()=%d\r\n", ptra->use_count());
  82. }
  83. int main(int argc, char **argv)
  84. {
  85.     if (1)
  86.     {
  87.         /*
  88.         本函数演示将shared_ptr作为函数参数时,shared_ptr的状态和行为模式变化。
  89.         */
  90.         cls *a = new cls();
  91.         std::shared_ptr<cls> ptr(a, std::default_delete<cls>());
  92.         func(&ptr);
  93.     }
  94.     if (0)
  95.     {
  96.         /*
  97.         构建std::shared_ptr的几种方法
  98.         1、使用std::make_shared
  99.         2、使用一个shared_ptr初始化另一个shared_ptr
  100.         3、使用shared_ptr.reset()
  101.         4、使用堆上的裸指针初始化shared_ptr
  102.         从本实例也可以看出,在vs2017编译的程序上,栈上对象的销毁顺序是与他们的声明顺序刚好相反。
  103.         */
  104.         cls a, *b = new cls(), *c = new cls();
  105.         printf("&a=%x,b=%x,c=%x\r\n", &a, b, c);
  106.         std::shared_ptr<cls> ptra = std::make_shared<cls>();//调用了a的无参构造函数
  107.         std::shared_ptr<cls> ptrb = std::make_shared<cls>(a);//调用了a的拷贝构造函数
  108.         std::shared_ptr<cls> ptra2(ptra);   //调用的是std::shared_ptr<T>的拷贝构造函数
  109.         std::shared_ptr<cls> ptra3;     ptra3.reset(b);     //调用shared_ptr<T>.reset()方法
  110.         std::shared_ptr<cls> ptra4(c);      //使用堆上的指针初始化std::shared_ptr
  111.         printf("ptra.use_count()=%d\r\n", ptra.use_count());
  112.         printf("ptra.get()=%x\r\n", ptra.get());
  113.         printf("ptrb.get()=%x\r\n", ptrb.get());
  114.         printf("ptra2.get()=%x\r\n", ptra2.get());
  115.         printf("ptra3.get()=%x\r\n", ptra3.get());
  116.         printf("ptra4.get()=%x\r\n", ptra4.get());
  117.     }
  118.     if (0)
  119.     {
  120.         /*
  121.         这种写法会引起程序假死,不管a是自定义类型还是基础类型。
  122.         当shared_ptr指向的指针内存被释放掉之后,在执行shared_ptr的析构函数,就会假死。
  123.         即://ERROR!!! 当将a托管给shared_ptr之后,a就不可以手动删除。
  124.         即:不能用栈上的对象去初始化std::shared_ptr。
  125.         */
  126.         cout << "--------------------------" << endl;
  127.         cls a;
  128.         std::shared_ptr<cls> ptra3(&a);
  129.     }
  130.     if (0)
  131.     {
  132.         /*
  133.         智能指针会托管交给他的对象,当将a托管给shared_ptr之后,a就不可以手动删除。
  134.         */
  135.         cout << "--------------------------" << endl;
  136.         cls *a = new cls();
  137.         std::shared_ptr<cls> ptra(a);
  138.         printf("ptra.use_count()=%d\r\n", ptra.use_count());
  139.         //delete a; a = nullptr;//ERROR!!! 
  140.         printf("ptra.use_count()=%d\r\n", ptra.use_count());
  141.         cls *pa = ptra.get();
  142.         printf("*pa=%d\r\n", pa);
  143.     }
  144.     if (0)
  145.     {
  146.         /*
  147.         本示例演示给std::shared_ptr<>设置删除器
  148. 1、默认删除器
  149. 2、lambda删除器
  150. 3、函数指针删除器
  151. 4、仿函数删除器
  152.         */
  153.         cout << "--------------------------" << endl;
  154.         cls *a = new cls();
  155.         std::shared_ptr<cls> ptr(a, std::default_delete<cls>());
  156.         cls *b = new cls();
  157.         std::shared_ptr<cls> ptr2(b, [](cls *x)->void {
  158.             printf("in lambda deleter\r\n");
  159.             delete x;
  160.         });
  161.         cls *c = new cls();
  162.         std::shared_ptr<cls> ptr3(c,func_delete);
  163.         cls *d = new cls();
  164.         std::shared_ptr<cls> ptr4(d, functor());
  165.         std::shared_ptr<cls> ptr5(ptr4);
  166.         /*
  167.         一组可能的打印如下:注意一下,如果使用函数对象作为析构器,要进行高达4此构造!
  168.         --------------------------
  169.         cls(), this=608100
  170.         cls(), this=608130
  171.         cls(), this=607ef0
  172.         cls(), this=6081f0
  173.         functor(), this=1ff67c
  174.         functor(functor && a), this=1ff578
  175.         functor(functor && a), this=1ff44c
  176.         functor(functor && a), this=60d024
  177.         ~functor(), this=1ff44c
  178.         ~functor(), this=1ff578
  179.         ~functor(), this=1ff67c
  180.         in functor deleter, this=60d024
  181.         ~cls(), this=6081f0
  182.         ~functor(), this=60d024
  183.         in function deleter
  184.         ~cls(), this=607ef0
  185.         in lambda deleter
  186.         ~cls(), this=608130
  187.         ~cls(), this=608100
  188.         --------------------------
  189.         */
  190.     }
  191.     cout << "--------------------------" << endl << endl;
  192.     return 0;
  193. }
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号