C++-指针-创建“智能指针”函数1:make_unique【创建并返回unique_ptr至指定类型的对象】【定义在memory.h标准头文件中】【reset、release、get】

一、使用make_unique获取unique_ptr类型的“智能指针”

  1. make_unique 同 unique_ptr 、auto_ptr 等一样,都是 smart pointer,可以取代new 并且无需 delete pointer,有助于代码管理。
  2. make_unique 创建并返回 unique_ptr 至指定类型的对象,这一点从其构造函数能看出来。make_unique相较于unique_ptr 则更加安全
  3. 编译器不同,make_unique 要求更新(Visual Studio 2015)。

关于make_unique的构造及使用例程,MSDN的讲解非常详细 (<memory> functions),以下是关于 make_unique 与make_shared 的知识介绍 :

条款21:尽量使用std::make_unique和std::make_shared而不直接使用new

让我们从对齐std::make_unique 和 std::make_shared这两块开始。

std::make_shared是c++11的一部分,但很可惜std::make_unique不是。它是在c++14里加入标准库的。假如你在使用c++11,也别担心,你很容易写出一个基本的版本。看这里:

template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&&... params){
    return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}

正如你看到的,make_unique完美传递了参数给对象的构造函数,从一个原始指针构造出一个std::unique_ptr,返回创建的std::unique_ptr。这个形式的函数不支持数组和定制删除器(见条款18),但它证明了一点点的努力就可以根据需要创建一个make_unique。要记住的是不要把你的版本放到std命名空间里,因为你不想当升级到c++14后会和库提供的标准实现冲突吧。

 std::make_unique 和 std::make_shared是三个make函数中的两个,make函数用来把一个任意参数的集合完美转移给一个构造函数从而生成动态分配内存的对象,并返回一个指向那个对象的灵巧指针。

第三个make是std::allocate_shared。它像std::make_shared一样,除了第一个参数是一个分配器对象,用来进行动态内存分配。

优先使用make函数的第一个原因即使用最简单的构造灵巧指针也能看出来。考虑如下代码:

auto upw1(std::make_unique<Widget>());     // with make func
std::unique_ptr<Widget> upw2(new Widget); // without make func
 
 
auto spw1(std::make_shared<Widget>());     // with make func
std::shared_ptr<Widget> spw2(new Widget); // without make func

我标注了基本的区别:

使用new的版本重复了被创建对象的键入,但是make函数则没有。重复类型违背了软件工程的一个重要原则:应该避免代码重复,代码中的重复会引起编译次数增加,导致目标代码膨胀,最终产生更难以维护的代码,通常会引起代码不一致,而不一致经常导致bug产生。另外,输入两次比输入一次要费力些,谁都想减少敲键盘的负担。

二、函数

1、get获取原始指针

std::unique_ptr<int> a = std::make_unique<int>(666);

int* b = a.get();
std::cout << b << std::endl;

2、reset释放智能指针

std::unique_ptr<int> a = std::make_unique<int>(666);

//释放内存,同时将a置0,所以不会出现悬挂指针的问题
a.reset();
std::cout << a << std::endl;

3、release将指针置0

std::unique_ptr<int> a = std::make_unique<int>(666);

// 虽然这个函数名叫release,但是并不会真的释放内存,只是把指针置0
// 而原来的那片装着666的内存依然存在,但是该函数会返回装着666的内存地址
// 综上:相当于先get,然后再reset
int* b = a.release();
std::cout << a << std::endl;
std::cout << b << std::endl;

C++自学24:唯一智能指针(make_unique/unique_ptr/reset/release/get/13.1)_文天养了三只抱抱的博客-CSDN博客_make_unique

智能指针make_unique 与make_shared 的知识介绍_aFakeProgramer的博客-CSDN博客_make_unique<memory> functions

优先使用make_unique 和 make_shared - make_wheels - 博客园

C++的std::make_unique是一个函数模板,用于动态分配对象返回一个std::unique_ptr智能指针。它接受构造函数的参数,并将它们完美转发给要创建对象的构造函数。\[1\] 使用std::make_unique可以避免手动调用new操作符来分配内存,并且在对象创建后,会自动管理内存的释放。这样可以避免内存泄漏和手动释放内存的麻烦。 下面是一个示例代码片段,演示了如何使用std::make_unique函数动态分配对象: ```cpp #include <memory> #include <iostream> class MyClass { public: MyClass(int a, float b) { std::cout << "Constructor called with parameters: " << a << ", " << b << std::endl; // Other initialization code... } }; int main() { // Create a unique_ptr type pointer managed by std::make_unique function auto ptr = std::make_unique<MyClass>(10, 20.5); // Use the created object // ... return 0; } ``` 在上面的示例,我们使用std::make_unique创建一个指向MyClass对象的std::unique_ptr指针。通过传递参数10和20.5,我们调用了MyClass的构造函数,并创建了一个MyClass对象。\[1\] 总结起来,std::make_unique是一个方便的函数模板,用于动态分配对象返回一个std::unique_ptr智能指针,它可以简化内存管理的过程。\[1\] #### 引用[.reference_title] - *1* [std::make_unique 使用](https://blog.csdn.net/oHeHui1/article/details/130790011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C++11总结](https://blog.csdn.net/zhzhangnews/article/details/100565986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值