c++ 智能指针的使用
https://www.cnblogs.com/TenosDoIt/p/3456704.html
#include <QCoreApplication>
#include <string>
#include <iostream>
#include <memory> using namespace std; class Test
{
public:
Test(string s)
{
str = s;
cout<<"Test creat : " << str << endl;
}
~Test()
{
cout<<"Test delete:"<<str<<endl;
}
string& getStr()
{
return str;
}
void setStr(string s)
{
str = s;
}
void print()
{
cout<<str<<endl;
}
private:
string str;
}; unique_ptr<Test> fun()
{
return unique_ptr<Test>(new Test(""));
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); auto_ptr<Test> ptest0(new Test(""));
ptest0->setStr("hello ");
ptest0->print();
ptest0.get()->print();
ptest0->getStr() += "world !";
(*ptest0).print();
ptest0.reset(new Test(""));
ptest0->print();
cout << "\n"; unique_ptr<Test> ptest1(new Test(""));
unique_ptr<Test> ptest2(new Test(""));
ptest1->print();
ptest2 = std::move(ptest1);//不能直接ptest2 = ptest1
if(ptest1 == NULL)cout<<"ptest = NULL\n";
Test* p = ptest2.release();
p->print();
ptest1.reset(p);
ptest1->print();
ptest2 = fun(); //这里可以用=,因为使用了移动构造函数
ptest2->print();
cout << "\n"; shared_ptr<Test> ptest(new Test(""));
shared_ptr<Test> ptest23(new Test(""));
cout<<ptest23->getStr()<<endl;
cout<<ptest23.use_count()<<endl;
ptest = ptest23;//"456"引用次数加1,“123”销毁
ptest->print();
cout<<ptest23.use_count()<<endl;//
cout<<ptest.use_count()<<endl;//
ptest.reset();
ptest23.reset();//此时“456”销毁
cout<<"done !\n"; return a.exec();
}
c++ 智能指针的使用的更多相关文章
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- C++11智能指针读书笔记;
智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
随机推荐
- [转帖]MySQL latch小结
MySQL latch小结 https://www.cnblogs.com/liang545621/p/9439816.html 学习一下 一个是数据库内容 一个是内存内容 与oracle的读写锁 应 ...
- 八皇后问题——列出所有的解,可推至N皇后
<数据结构>--邓俊辉版本 读书笔记 今天学习了回溯法,有两道习题,一道N皇后,一道迷宫寻径.今天,先解决N皇后问题.由于笔者 擅长java,所以用java重现了八皇后问题. 注意是jav ...
- CodeForces-1159B-Expansion coefficient of the array
B. Expansion coefficient of the array time limit per test:1 second memory limit per test:256 megabyt ...
- 关于JavaScript 基础总结
Dom:document object model 即文档对象模型 Bom: browser object model 即浏览器对象模型 文档对象模型即与文本密切相关,比如document.query ...
- Android studio 3.1.3真机调试报错,no target device found
Android studio 3.1.2 的 Android monitor 改为 Android profiler,直接点这个就可以真机调试,在手机安装相应app 如果不行,报错,"no ...
- Android--圆角背景style
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- cf 869c The Intriguing Obsession
题意:有三种三色的岛,用a,b,c来标识这三种岛.然后规定,同种颜色的岛不能相连,而且同种颜色的岛不能和同一个其他颜色的岛相连.问有多少种建桥的方法. 题解:em....dp.我们先看两个岛之间怎么个 ...
- Vue+VSCode开发环境搭建
时间2019.9月 1. 安装 nodeJS; 安装VScode 安装好nodeJS之后,默认会安装好npm 测试 nodeJS 和npm是否可以执行 node -v npm -v 1.1 VScod ...
- js实现CheckBox全选或者不全选
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">< ...
- springboot_2
1. 配置文件简介 spring boot使用一个全局配置文件:application.properties或者application.yml,放置在src/main/resources目录下或者类路 ...