/* 智能指针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. AngularJS checkbox/复选框 根据缓存数据实时显示

    想缓存选择按钮时,可以使用这样的方法. index.html <!DOCTYPE html> <html data-ng-app="App"> <he ...

  2. loj#2015. 「SCOI2016」妖怪 凸函数/三分

    题目链接 loj#2015. 「SCOI2016」妖怪 题解 对于每一项展开 的到\(atk+\frac{dnf}{b}a + dnf + \frac{atk}{a} b\) 令$T = \frac{ ...

  3. 潭州课堂25班:Ph201805201 爬虫基础 第五课 (案例) 豆瓣分析 (课堂笔记)

    动态讲求 , 翻页参数: # -*- coding: utf-8 -*- # 斌彬电脑 # @Time : 2018/9/1 0001 3:44 import requests,json class ...

  4. 重写alert方法,去掉地址显示

    //重写alert方法,去掉地址显示window.alert = function(name){ var iframe = document.createElement("IFRAME&qu ...

  5. 支付宝集成遇到"_EVP_DecodeBlock",referenced from:报错

    遇到问题报错如下: 调试了好多遍,始终不行,检测各种依赖库,发现并没有少什么.后来发现支付宝demo里比文档讲解里面多两个.a文件 直接加上就好了

  6. Pycharm中实现多个项目共存的方式

    一.背景 在Python学习中,使用pycharm只能打开一个项目,如果想在一个pycharm中同时打开多个项目,该怎么办呢?由于学习中遇到需要打开多个项目,所以就百度查询了一下方法. 二.解决办法 ...

  7. File构建实例的路径:绝对路径和相对路径

    public static void main(String[] args) throws Exception { File file = new File("bin/dyan.txt&qu ...

  8. v$instance如何生成

    参考:http://www.itpub.net/thread-1284858-1-1.html 1.ORACLE 先创建的x$ 表即RDBMS的内部表 2.然后在X$表的基础上创建了GV$ 视图.  ...

  9. C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie

    网上找到的可以完整获取Cookie的方法,转载一下希望能帮助更多人. 亲测可用 在Winform中使用WebBrowser控件获取网站的Cookie有时候是不完整的,默认调用Document.Cook ...

  10. mac环境下intellij的自定义配置文件位置

    ~/Library/Preferences/IntelliJIdea2017.2/