C++11 std::unique_ptr

该代码示例展示了如何在C++中使用unique_ptr管理对象生命周期,特别是与多态基类和派生类一起使用的情况。unique_ptr被用来创建和存储CFather和CMother对象,它们都继承自CBase。在循环中,unique_ptr将对象推入vector容器,并通过多态调用PrintInfo方法显示不同的输出。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <memory>
#include <list>
#include <vector>
using namespace std;
记录一下unique_ptr的用法
class CBase
{
public:
    CBase(){printf("Base create..\r\n");}
    virtual ~CBase(){printf("Base delete\r\n");}
    virtual void PrintInfo(){printf("Base class print..\r\n");}
};

class CFather : public CBase
{
public:
     CFather(){printf("Father create..\r\n");}
     ~CFather(){printf("Father delete..\r\n");}

     virtual void PrintInfo() override {printf("Father class print..\r\n");}
};

class CMother : public CBase
{
public:
     CMother(){printf("Mother create..\r\n");}
     ~CMother(){printf("Mother delete..\r\n");}

     virtual void PrintInfo() override {printf("Mother class print..\r\n");}
};

int main()
{
   int flag = 5;
   while(flag --)
   {
    	/*设置数组*/
        std::vector<std::unique_ptr<CBase>> vecBase;
        for(int i = 0;i<4;i++)
        {
            std::unique_ptr<CBase> base;
            /*交替压入father,mother类*/
            if(i % 2 == 0)
                vecBase.push_back(std::unique_ptr<CBase>(new CFather));
            else
                vecBase.push_back(std::unique_ptr<CBase>(new CMother));
        }

        std::vector<std::unique_ptr<CBase>>::iterator iter = vecBase.begin();
        for(;iter != vecBase.end();iter++)
        {
            /*多态打印*/
            (*iter)->PrintInfo();
        }
   }

    return 0;
}
std::unique_ptrC++标准库中的一个智能指针类,用于管理动态分配的对象的所有权。它提供了独占式拥有(exclusive ownership)语义,意味着一个unique_ptr实例可以独占地拥有所指向的对象,并在其生命周期结束时自动释放该对象。 使用std::unique_ptr的主要优点是它提供了自动的内存管理,无需手动调用delete来释放对象。当unique_ptr超出其作用域或被重新分配时,它会自动删除所拥有的对象。 以下是std::unique_ptr的一些重要特性和用法: 1. 创建std::unique_ptr对象: ``` std::unique_ptr<int> ptr(new int); ``` 在这个例子中,我们创建了一个指向int类型对象的unique_ptr。 2. 转移所有权: ``` std::unique_ptr<int> ptr1(new int); std::unique_ptr<int> ptr2 = std::move(ptr1); ``` 通过std::move函数,我们可以将ptr1的所有权转移到ptr2中。此时,ptr1将不再拥有对象的所有权,ptr2将成为唯一拥有者。 3. 访问所指向的对象: ``` std::unique_ptr<int> ptr(new int(42)); int value = *ptr; ``` 我们可以通过解引用操作符*来访问unique_ptr所指向的对象。 4. 释放所有权: ``` std::unique_ptr<int> ptr(new int); int* rawPtr = ptr.release(); ``` 调用release函数可以释放unique_ptr对对象的所有权,返回一个指向该对象的裸指针。此时,unique_ptr不再拥有对象的所有权。 5. 重置unique_ptr: ``` std::unique_ptr<int> ptr(new int); ptr.reset(new int); ``` 使用reset函数可以将unique_ptr指向一个新的对象。这将导致原来指向的对象被删除,并将unique_ptr重新指向新对象。 6. 自定义删除器: ``` std::unique_ptr<FILE, decltype(&std::fclose)> filePtr(std::fopen("file.txt", "r"), &std::fclose); ``` unique_ptr允许我们提供一个自定义的删除器函数,以便在释放对象时执行其他操作。在上述示例中,我们使用std::fclose函数作为删除器来关闭打开的文件。 总之,std::unique_ptr提供了一种安全、高效的方式来管理动态分配对象的所有权,并避免了手动内存管理所带来的错误和资源泄漏的风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值