C++ unique_ptr
时间: 2023-12-19 21:05:45 浏览: 36
C++11中的unique_ptr是一种智能指针,它提供了一种独占式拥有其所指向对象的所有权的语义,即同一时间内只能有一个unique_ptr指向一个对象。当unique_ptr被销毁时,它所指向的对象也会被销毁。unique_ptr的使用可以避免内存泄漏和悬空指针等问题。
以下是unique_ptr的基本使用方法:
1. 创建unique_ptr对象
```c++
std::unique_ptr<int> p(new int(10)); // 创建一个指向int类型对象的unique_ptr
```
2. 获取unique_ptr指向的对象
```c++
int a = *p; // 获取unique_ptr指向的对象的值
```
3. 释放unique_ptr指向的对象
```c++
p.reset(); // 释放unique_ptr指向的对象
```
4. 将unique_ptr转移给其他unique_ptr
```c++
std::unique_ptr<int> p1 = std::move(p); // 将p指向的对象的所有权转移给p1
```
需要注意的是,由于unique_ptr的独占性质,不能将一个unique_ptr赋值给另一个unique_ptr或者将一个unique_ptr作为参数传递给一个函数,除非使用std::move进行转移。
相关问题
C++手撕unique_ptr
unique_ptr 是 C++11 标准提供的智能指针,用于管理动态分配的对象。它使用了独占所有权的概念,保证了在不再需要指针时自动释放资源,从而避免了内存泄漏。
手撕 unique_ptr 的实现涉及到指针的拷贝和移动语义。下面是一个简单的手撕 unique_ptr 的示例代码:
```cpp
template <typename T>
class unique_ptr {
public:
explicit unique_ptr(T* ptr = nullptr) : ptr_(ptr) {}
~unique_ptr() {
delete ptr_;
}
unique_ptr(const unique_ptr&) = delete; // 禁用拷贝构造函数
unique_ptr& operator=(const unique_ptr&) = delete; // 禁用拷贝赋值运算符
unique_ptr(unique_ptr&& other) noexcept : ptr_(other.ptr_) {
other.ptr_ = nullptr;
}
unique_ptr& operator=(unique_ptr&& other) noexcept {
if (this != &other) {
delete ptr_;
ptr_ = other.ptr_;
other.ptr_ = nullptr;
}
return *this;
}
T& operator*() const {
return *ptr_;
}
T* operator->() const {
return ptr_;
}
T* get() const {
return ptr_;
}
private:
T* ptr_;
};
```
上述代码中,我们定义了一个类模板 `unique_ptr`,它模拟了标准库中的 `std::unique_ptr` 功能。在构造函数中,我们接受一个指针作为参数,并将其保存在 `ptr_` 成员变量中。析构函数负责释放指针所指向的资源。为了遵循独占所有权的原则,我们禁用了拷贝构造函数和拷贝赋值运算符,而使用移动语义来实现赋值操作。`operator*` 和 `operator->` 用于重载解引用操作符,方便使用指针指向的对象。
需要注意的是,上述实现只是一个简单的手撕版 unique_ptr,并没有处理更复杂的边界情况和异常安全性。在实际使用中,建议使用标准库提供的 `std::unique_ptr`,它已经经过了充分测试和优化。
unique_ptr c++代码
下面是一个使用 `unique_ptr` 的 C++ 代码示例:
```c++
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> p(new int(10)); // 使用 unique_ptr 动态分配内存
std::cout << *p << std::endl; // 输出内存中的值
return 0;
}
```
在这个例子中,我们使用 `std::unique_ptr` 对一个 int 类型的内存进行动态分配,然后输出内存中的值。`std::unique_ptr` 是一个智能指针,它可以确保在离开作用域时自动释放分配的内存,避免内存泄漏和野指针的问题。在使用 `std::unique_ptr` 时,我们只需要在定义时将动态分配的内存地址传递给它,然后就可以通过指针操作内存了。在离开作用域时,`std::unique_ptr` 会自动释放内存,无需手动调用 `delete`。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)