/* 智能指针unique_ptr */

#include <iostream>
#include <string>
#include <memory>
#include <vector> /*
unique_ptr 独占所指向的对象, 同一时刻只能有一个 unique_ptr 指向给定对象(通过禁止拷贝语义, 只有移动语义来实现),
定义于 memory (非memory.h)中, 命名空间为 std. unique_ptr 不支持拷贝和赋值. */ struct Student
{
public:
Student(std::string name_, int age_) :name(name_), age(age_) {}
std::string name;
int age; }; std::unique_ptr<Student> test_clone()
{
std::unique_ptr<Student> up1 = std::make_unique<Student>("spaow",);
return up1;
} struct STDeleter
{
void operator() (int* obj)
{
if (obj)
{
for (int i = ; i < ; i++)
{
//obj[i] = i + 2;
printf("obj[i] = %d\n", obj[i]);
}
free(obj);
obj = NULL;
}
}
}; void test()
{
//初始化方式一
std::unique_ptr<Student> up1 = std::make_unique<Student>("tom",); //error unique_ptr 不支持拷贝构造函数
//std::unique_ptr<Student> up2(up1); //error unique_ptr 不支持赋值
//std::unique_ptr<Student> up3 = up1; //初始化方式二
std::unique_ptr<Student> up4(new Student("jack", )); //初始化方式三:
/*
release方法: 放弃内部对象的所有权,将内部指针置为空, 返回所内部对象的指针, 此指针需要手动释放
*/
std::unique_ptr<Student> up5(up1.release()); //初始化方式四
/*
reset方法: 销毁内部对象并接受新的对象的所有权(如果使用缺省参数的话,也就是没有任何对象的所有权, 此时仅将内部对象释放, 并置为空)
*/
std::unique_ptr<Student> up6;
up6.reset(new Student("als", )); //成员函数的使用 //可以进行移动构造和移动赋值操作
std::unique_ptr<Student> up7;
up7 = std::move(up6); std::unique_ptr<Student> up8(std::move(up7)); //特殊的拷贝
std::unique_ptr<Student> up9 = test_clone(); printf("name is [%s] .\n",up9->name.c_str()); //在容器中保存指针
std::vector<std::unique_ptr<Student> > vec;
std::unique_ptr<Student> upTmp(new Student("kld",));
vec.push_back(std::move(upTmp)); //unique_ptr 支持管理数组
std::unique_ptr<int[]> ups(new int[]); printf("sizeof(ups) = %d\n", sizeof(ups));//打印4,并非数组实际长度 for (int i = ; i < ; i++)
{
ups[i] = i;
printf("ups[i] = %d\n", ups[i]);
} //自定义删除器定义
int *tempArr = (int *)malloc(sizeof(int) * );
std::unique_ptr<int, STDeleter> usp2(tempArr, STDeleter()); int *pcIndex = usp2.get();
for (int i = ; i < ; i++)
{
pcIndex[i] = i+;
} } int main()
{
test();
getchar();
return ;
}

C++ 智能指针六的更多相关文章

  1. 【C++】智能指针简述(六):智能指针总结及补充

    本文我们主要来总结一下前文介绍过的智能指针相关原理及实现,顺便补充一下前文未提到的shared_ptr删除器部分的内容. 总结: 1.智能指针,通过RAII机制,构造对象时完成资源的初始化,析构对象时 ...

  2. C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

    Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...

  3. c++ auto_ptr智能指针

    c++ auto_ptr智能指针 该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用. 使用方法: auto_ptr& ...

  4. C++智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

  5. STL模板_智能指针概念

    一.智能指针1.类类型对象,在其内部封装了一个普通指针.当智能指针对象因离开作用域而被析构时,其析构函数被执行,通过其内部封装的普通指针,销毁该指针的目标对象,避免内存泄露.2.为了表现出和普通指针一 ...

  6. 【转】C++智能指针简单剖析

    原文链接:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看 <C++ Primer Plus>第六版,这的确是本好书 ...

  7. 【C++】智能指针简单剖析

    转自 http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中 ...

  8. c/c++ 智能指针 shared_ptr 使用

    智能指针 shared_ptr 使用 上一篇智能指针是啥玩意,介绍了什么是智能指针. 这一篇简单说说如何使用智能指针. 一,智能指针分3类:今天只唠唠shared_ptr shared_ptr uni ...

  9. [转]C++智能指针简单剖析

    C++智能指针简单剖析  https://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版 ...

随机推荐

  1. (转)【Java线程】Java内存模型总结

    Java的并发采用的是共享内存模型(而非消息传递模型),线程之间共享程序的公共状态,线程之间通过写-读内存中的公共状态来隐式进行通信.多个线程之间是不能直接传递数据交互的,它们之间的交互只能通过共享变 ...

  2. javaweb中为mysql的curd多个值的语句

    更新语句 String sql = "update student set num=?,name=?,birthday=?,score=?,password=? where id=?&quo ...

  3. C++ new

    //#include "stdafx.h" #include <iostream> using namespace std; int main() { , n = , ...

  4. SLAM(二)----学习资料下载

    有位师兄收集了很多slam的学习资料, 做的很赞, 放到了github上, 地址:https://github.com/liulinbo/slam.git ruben update 0823 2016 ...

  5. springboot中velocity tool中注入bean

    在使用springboo的时候,遇到一个问题,想在tool类中注入一个bean,一直失败,翻了下源码,是因为工具类的初始化方法为反射class调用newInstance方法,详见 http://www ...

  6. 什么是less?

    什么是less? 简单的说,你可以在你的css文件中使用变量.函数等方式来编写你的css. less具有哪些功能? 混入(Mixins)——class中的class: 参数混入——可以传递参数的cla ...

  7. Android-ContentProvider使用

    Android-ContentProvider使用 一 建立ContentProviderserver端 1 建立一个继承自ContentProvider的类并重写接口方法(这里仅打一些log做代表) ...

  8. OEMbutton乱码问题解决

    一.出现故障: 在Linux环境中安装tid=12">Oracle 10g,启用EM时.出现button显示乱码现象,例如以下: 二.分析问题: 由于在安装Oracle10g时,JDK ...

  9. angularjs drag and drop

    angular-dragula Drag and drop so simple it hurts 480 live demo angular-drag-and-drop-lists Angular d ...

  10. 开发指南专题五:JEECG微云高速开发平台代码生成器

    开发指南专题五:JEECG微云高速开发平台代码生成器 1.1. Maven开发环境搭建 在搭建jeecg的maven开发环境之前,须要先配置好本机的maven环境,并在eclipse中安装好m2ecl ...