C++ 智能指针 shared_ptr、make_shared用法
一、使用shared_ptr条件
C++版本11以上
需引入头文件
#include <memory>
否则编译会报错
error: ‘shared_ptr’ was not declared in this scope
二、用法
#include <iostream>
#include <vector>
#include <memory>
using namespace std;class testClass
{public: testClass(){temp3 = make_shared<vector<int>>();}shared_ptr<vector<int>> temp3;
};int main() {cout<<"999999"<<endl;testClass tempObj;cout<<"temp1的长度: "<<tempObj.temp3->size()<<endl;tempObj.temp3->push_back(333);cout<<"temp1的长度: "<<tempObj.temp3->size()<<endl;return 0;
}
三、若是定义在class中,则需要在class的构造函数中
#include <iostream>
#include <vector>
#include <memory>
using namespace std;class testClass
{public: testClass(){temp3 = make_shared<vector<int>>();}shared_ptr<vector<int>> temp3;
};int main() {cout<<"999999"<<endl;testClass tempObj;cout<<"temp1的长度: "<<tempObj.temp3->size()<<endl;tempObj.temp3->push_back(333);cout<<"temp1的长度: "<<tempObj.temp3->size()<<endl;return 0;
}
原文:C++ 智能指针 shared_ptr、make_shared用法
C++ 智能指针 shared_ptr、make_shared用法相关推荐
- 智能指针shared_ptr的用法
智能指针shared_ptr的用法 2016-12-03 15:39 by jiayayao, 360 阅读, 0 评论, 收藏, 编辑 为了解决C++内存泄漏的问题,C++11引入了智能指针(Sma ...
- C++11智能指针shared_ptr、weak_ptr、unique_ptr用法
该博文为原创文章,未经博主同意不得转载,如同意转载请注明博文出处 本文章博客地址:https://cplusplus.blog.csdn.net/article/details/105065859 智 ...
- get方法报空指针_智能指针shared_ptr踩坑笔记
平时写代码一直避免使用指针,但在某些场景下指针的使用还是有必要的.最近在项目中简单使用了一下智能指针(shared_ptr),结果踩了不少坑,差点就爬不出来了.痛定思痛抱着<Cpp Primer ...
- 智能指针shared_ptr的原理、用法和注意事项
智能指针shared_ptr的原理.用法和注意事项 1 前言 2 shared_ptr原理 3 shared_ptr的基本用法 3.1 初始化 3.2 获取原始指针 4 智能指针和动态数组 4.1 c ...
- 智能指针shared_ptr
如果有可能就使用unique_ptr,然后很多时候对象是需要共享的,因此shared_ptr也就会用得很多.shared_ptr允许多个指向同一个对象,当指向对象的最后一个shared_ptr销毁时, ...
- 智能指针shared_ptr的几个例子
#include <string> #include <iostream> #include <memory> //智能指针定义在头文件memory中,例如shar ...
- C++智能指针shared_ptr、unique_ptr以及weak_ptr
目录 shared_ptr类 shared_ptr和unique_ptr都支持的操作 shared_ptr独有的操作 make_shared函数 shared_ptr自动销毁所管理的对象 由普通指针管 ...
- 智能指针(shared_ptr、unique_ptr、weak_ptr)的使用
智能指针的使用 一.shared_ptr 1.创建一个shared_ptr 2.shared_ptr的常用成员函数 reset成员函数的使用 3.==注意事项== 二.unique_ptr 1.uni ...
- C++ 使用智能指针shared_ptr/unique_ptr管理数组
目录 零.要管理的类 一.使用shared_ptr管理数组 二.使用unique_ptr管理数组 1.第一种方式 2.第二种方式 关于shared_ptr/unique_ptr的基础,我不在本篇博客中 ...
最新文章
- 推荐三款scrum看板协作工具
- [BZOJ3631][JLOI2014]松鼠的新家
- Bitdefender Internet Security 2013 – 免费3个月
- 前端面试8:渲染机制
- 三菱q系列plc连接电脑步骤_三菱Q系列PLC与三菱变频器的CC-link通讯技术(我的学习笔记)...
- 第7期 Datawhale 组队学习计划
- 培养创造力的10个注意点
- 今天,Java 12 正式发布了!
- Python之旅Day6 模块应用
- java redirect 超时_java – Spring Security 3.0重定向到超时的页面
- ASP.NET数据分页技术(4)
- html 点击展开全部,纯CSS实现点击展开全文功能
- AMAZON SP API Feed
- 使用Kinect V2进行录制视频
- Windows下架设自己的DNS服务器
- 学习PHP 第七天 die() 函数,及Mysql的操作
- web前端(css3)
- 解决IDEA输出中文显示为问号
- twisted-02 ChatRoom
- MapReduce案例5——求互粉好友对
热门文章
- ATmega88的简单引脚设置
- python3.6环境下安装gevent,附协程TCP服务器客户端代码
- I3S向3DTiles数据格式的转换
- html渐变线条代码,css3线性渐变语法的详解(代码示例)
- 轻薄至极的LED透明屏
- 元数据编程将编程语言的语义提高了一个层次
- 威纶和s7200通讯线_S7-200作为从站用一个通讯口同时与组态王和维纶触摸屏通讯,用PPI协议,可以吗?...
- 标准15针 VGA 显示接口定义及焊接方法
- linux游戏星际公民,《星际公民》推出全新DLC包 售价高达2.7万美元
- 机器人图形变变变_《图形变变变》中班教案