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 ...
随机推荐
- Recurrent neural network (RNN) - Pytorch版
import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms # ...
- [Xamarin] - Xamarin.Forms Project with .Net Standard 2.0
1. Install .NET Core 2.0 SDK .https://www.microsoft.com/net/download/core 2. Install Android 7.1 (AP ...
- Word 下划线无法对齐?用表格替代下划线(论文封面必备)
1. 前言 在使用Word排版制作合同或者论文封面时,我们可能会使用一些下划线,但是,你在下划线上输入内容后,发现下划线会随着内容而增长,根本无法与上下的下划线对齐.有什么好办法可以解决这一问题呢?其 ...
- cf 869c The Intriguing Obsession
题意:有三种三色的岛,用a,b,c来标识这三种岛.然后规定,同种颜色的岛不能相连,而且同种颜色的岛不能和同一个其他颜色的岛相连.问有多少种建桥的方法. 题解:em....dp.我们先看两个岛之间怎么个 ...
- 在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列)
原文:在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 1.如果去掉这个临时表中合计为0 ...
- mvc伪静态
方法一:IIS配置伪静态 方法二:项目配置伪静态 网站配置文件Web.config <system.webServer> <handlers> <add name=&qu ...
- ABP 基于DDD的.NET开发框架 学习(二)创建实体
1.创建模型类打开.Core项目,新建新建一个项目文件夹(Demo);为了演示表关联及外键的使用,创建两个类:创建类ClothesCategoty.csusing Abp.Domain.Entitie ...
- .netCore 简易Web 项目
static async Task Main(string[] args) { var _httpListener = new HttpListener(); _httpListener.Prefix ...
- Java Swing中文乱码解决方法
Run As Run Configuration,在Arguments中增加下面这句: -Dfile.encoding=gbk
- jenkins pipline
def getHost(){ def remote = [:] remote.name = 'server02' remote.host = '39.19.90' remote.user = 'roo ...