zoukankan      html  css  js  c++  java
  • C++ 智能指针六

    /* 智能指针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",65);
        return up1;
    }
    
    struct STDeleter
    {
        void operator() (int* obj)
        {
            if (obj)
            {
                for (int i = 0; i < 10; i++)
                {
                    //obj[i] = i + 2;
                    printf("obj[i] = %d
    ", obj[i]);
                }
                free(obj);
                obj = NULL;
            }
        }
    };
    
    void test()
    {
        //初始化方式一
        std::unique_ptr<Student> up1 = std::make_unique<Student>("tom",11);
    
        //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", 10));
    
        //初始化方式三:
        /*
        release方法: 放弃内部对象的所有权,将内部指针置为空, 返回所内部对象的指针, 此指针需要手动释放
        */
        std::unique_ptr<Student> up5(up1.release());
    
        //初始化方式四
        /*
         reset方法: 销毁内部对象并接受新的对象的所有权(如果使用缺省参数的话,也就是没有任何对象的所有权, 此时仅将内部对象释放, 并置为空)
        */
        std::unique_ptr<Student> up6;
        up6.reset(new Student("als", 12));
    
        //成员函数的使用
    
        //可以进行移动构造和移动赋值操作
        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] .
    ",up9->name.c_str());
    
        //在容器中保存指针
        std::vector<std::unique_ptr<Student> > vec;
        std::unique_ptr<Student> upTmp(new Student("kld",16));
        vec.push_back(std::move(upTmp));
    
        //unique_ptr 支持管理数组
        std::unique_ptr<int[]> ups(new int[10]);
    
        printf("sizeof(ups) = %d
    ", sizeof(ups));//打印4,并非数组实际长度
    
        for (int i = 0; i < 10; i++)
        {
            ups[i] = i;
            printf("ups[i] = %d
    ", ups[i]);
        }
    
        //自定义删除器定义
        int *tempArr = (int *)malloc(sizeof(int) * 10);
        std::unique_ptr<int, STDeleter> usp2(tempArr, STDeleter());
    
        int *pcIndex = usp2.get();
        for (int i = 0; i < 10; i++)
        {
            pcIndex[i] = i+2;
        }
    
    }
    
    int main()
    {
        test();
        getchar();
        return 0;
    }
  • 相关阅读:
    设计模式之工厂模式(Factory Pattern)用C++实现
    读书笔记之Effective C++ 1.让自己习惯C++
    LeetCode之RemoveDuplicates扩展
    一致性哈希(Consistent Hashing)原理和实现(整理)
    LeetCode之Remove Duplicates from Sorted Array
    C++小结
    HTTP、HTTP1.0、HTTP1.1、HTTP2.0——笔记
    《趣谈网络协议》——问答笔记
    技术源于生活(头脑风暴)
    解决:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/10294513.html
Copyright © 2011-2022 走看看