赞
踩
吐槽一下,现在CSDN的编辑器真实太大妈烂!
- 本实例演示了
- std::shared_ptr<T>的初始化的集中方法,
- 使用上不能使用栈上对象初始化
- 删除器的使用
- 函数传参的使用
- 等使用方法
- // smart-ptr.cpp: 定义控制台应用程序的入口点。
- //
-
-
- #include "stdafx.h"
- #include <iostream>
- #include <memory>
- using namespace std;
-
-
- class cls
- {
- public:
- cls()
- {
- printf("cls(), this=%x\r\n", this);
- }
- ~cls()
- {
- printf("~cls(), this=%x\r\n", this);
- }
- cls(const cls & a)
- {
- printf("cls(const cls & a), this=%x\r\n",this);
- }
- cls(cls && a)
- {
- printf("cls(cls && a), this=%x\r\n", this);
- }
- cls& operator=(const cls & a)
- {
- printf("cls& operator=(const cls &a), this=%x\r\n", this);
- }
- cls& operator=(const cls && a)
- {
- printf("cls& operator=(const cls && a), this=%x\r\n", this);
- }
- };
-
-
- void func_delete(cls *x)
- {
- printf("in function deleter\r\n");
- delete x;
- }
-
-
- class functor
- {
- public:
- functor()
- {
- printf("functor(), this=%x\r\n", this);
- }
- ~functor()
- {
- printf("~functor(), this=%x\r\n", this);
- }
- functor(const functor & a)
- {
- printf("functor(const functor & a), this=%x\r\n", this);
- }
- functor(functor && a)
- {
- printf("functor(functor && a), this=%x\r\n", this);
- }
- functor& operator=(const functor & a)
- {
- printf("functor& operator=(const functor &a), this=%x\r\n", this);
- }
- functor& operator=(const functor && a)
- {
- printf("functor& operator=(const functor && a), this=%x\r\n", this);
- }
- void operator()(cls *x)
- {
- printf("in functor deleter, this=%x\r\n", this);
- delete x;
- }
- };
-
-
- void func(std::shared_ptr<cls> *ptra)
- {
- printf("ptra.use_count()=%d\r\n", ptra->use_count());
- }
- int main(int argc, char **argv)
- {
- if (1)
- {
- /*
- 本函数演示将shared_ptr作为函数参数时,shared_ptr的状态和行为模式变化。
- */
- cls *a = new cls();
- std::shared_ptr<cls> ptr(a, std::default_delete<cls>());
- func(&ptr);
- }
- if (0)
- {
- /*
- 构建std::shared_ptr的几种方法
- 1、使用std::make_shared
- 2、使用一个shared_ptr初始化另一个shared_ptr
- 3、使用shared_ptr.reset()
- 4、使用堆上的裸指针初始化shared_ptr
- 从本实例也可以看出,在vs2017编译的程序上,栈上对象的销毁顺序是与他们的声明顺序刚好相反。
- */
- cls a, *b = new cls(), *c = new cls();
- printf("&a=%x,b=%x,c=%x\r\n", &a, b, c);
-
-
- std::shared_ptr<cls> ptra = std::make_shared<cls>();//调用了a的无参构造函数
- std::shared_ptr<cls> ptrb = std::make_shared<cls>(a);//调用了a的拷贝构造函数
- std::shared_ptr<cls> ptra2(ptra); //调用的是std::shared_ptr<T>的拷贝构造函数
- std::shared_ptr<cls> ptra3; ptra3.reset(b); //调用shared_ptr<T>.reset()方法
- std::shared_ptr<cls> ptra4(c); //使用堆上的指针初始化std::shared_ptr
-
-
- printf("ptra.use_count()=%d\r\n", ptra.use_count());
-
-
- printf("ptra.get()=%x\r\n", ptra.get());
- printf("ptrb.get()=%x\r\n", ptrb.get());
- printf("ptra2.get()=%x\r\n", ptra2.get());
- printf("ptra3.get()=%x\r\n", ptra3.get());
- printf("ptra4.get()=%x\r\n", ptra4.get());
- }
- if (0)
- {
- /*
- 这种写法会引起程序假死,不管a是自定义类型还是基础类型。
- 当shared_ptr指向的指针内存被释放掉之后,在执行shared_ptr的析构函数,就会假死。
- 即://ERROR!!! 当将a托管给shared_ptr之后,a就不可以手动删除。
- 即:不能用栈上的对象去初始化std::shared_ptr。
- */
- cout << "--------------------------" << endl;
- cls a;
- std::shared_ptr<cls> ptra3(&a);
- }
- if (0)
- {
- /*
- 智能指针会托管交给他的对象,当将a托管给shared_ptr之后,a就不可以手动删除。
- */
- cout << "--------------------------" << endl;
- cls *a = new cls();
- std::shared_ptr<cls> ptra(a);
- printf("ptra.use_count()=%d\r\n", ptra.use_count());
-
-
- //delete a; a = nullptr;//ERROR!!!
-
-
- printf("ptra.use_count()=%d\r\n", ptra.use_count());
-
-
- cls *pa = ptra.get();
- printf("*pa=%d\r\n", pa);
- }
- if (0)
- {
- /*
- 本示例演示给std::shared_ptr<>设置删除器
- 1、默认删除器
- 2、lambda删除器
- 3、函数指针删除器
- 4、仿函数删除器
- */
- cout << "--------------------------" << endl;
- cls *a = new cls();
- std::shared_ptr<cls> ptr(a, std::default_delete<cls>());
-
-
- cls *b = new cls();
- std::shared_ptr<cls> ptr2(b, [](cls *x)->void {
- printf("in lambda deleter\r\n");
- delete x;
- });
-
-
- cls *c = new cls();
- std::shared_ptr<cls> ptr3(c,func_delete);
-
-
- cls *d = new cls();
- std::shared_ptr<cls> ptr4(d, functor());
- std::shared_ptr<cls> ptr5(ptr4);
-
-
- /*
- 一组可能的打印如下:注意一下,如果使用函数对象作为析构器,要进行高达4此构造!
- --------------------------
- cls(), this=608100
- cls(), this=608130
- cls(), this=607ef0
- cls(), this=6081f0
- functor(), this=1ff67c
- functor(functor && a), this=1ff578
- functor(functor && a), this=1ff44c
- functor(functor && a), this=60d024
- ~functor(), this=1ff44c
- ~functor(), this=1ff578
- ~functor(), this=1ff67c
- in functor deleter, this=60d024
- ~cls(), this=6081f0
- ~functor(), this=60d024
- in function deleter
- ~cls(), this=607ef0
- in lambda deleter
- ~cls(), this=608130
- ~cls(), this=608100
- --------------------------
- */
- }
-
-
- cout << "--------------------------" << endl << endl;
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。