std::shared_ptr 变量如何先定义再赋值,使用怎么释放
时间: 2023-08-17 08:28:19 浏览: 70
可以通过以下方式先定义再赋值一个 std::shared_ptr 变量:
```
std::shared_ptr<int> ptr; // 先定义一个空的 shared_ptr 变量
ptr = std::make_shared<int>(42); // 再赋值一个指向 int 类型对象的 shared_ptr 变量
```
这样定义的 shared_ptr 变量可以像普通指针一样使用,例如可以通过 `*ptr` 访问指向的对象,或者通过 `ptr.get()` 获取指向的对象的原始指针。
对于使用 shared_ptr 变量的内存管理,通常无需手动释放内存。当 shared_ptr 变量的引用计数为 0 时,即没有任何指针指向该对象时,shared_ptr 会自动释放指向的内存。这意味着,当定义的 shared_ptr 变量超出作用域时,或者通过 `reset()` 方法清空 shared_ptr 变量时,指向的内存会被自动释放。此外,在使用 shared_ptr 时,不要使用原始指针来释放内存,否则会导致未定义行为。
相关问题
conversiion from 'std::_shared_ptr<int,(__gnu_cxx::_Lock_policy)2>::element_type*‘ {aka 'int*'} to non-scalar type 'std::shared_ptr<int>'requested
这个错误通常是因为您将一个指针类型的变量或参数,直接赋值给了 `std::shared_ptr` 类型的智能指针,而这是不允许的。
例如,下面的代码会导致这个错误:
```
int* p = new int(10);
std::shared_ptr<int> ptr = p; // 错误:不能将 int* 类型的指针直接赋值给 std::shared_ptr<int> 类型的智能指针
```
正确的做法是要使用智能指针的构造函数,将指针类型的变量或参数转换为智能指针类型:
```
int* p = new int(10);
std::shared_ptr<int> ptr(p); // 正确:使用 std::shared_ptr<int> 的构造函数将 int* 类型的指针转换为智能指针类型
```
或者,您也可以使用 `std::make_shared` 函数来创建智能指针:
```
int* p = new int(10);
std::shared_ptr<int> ptr = std::make_shared<int>(*p); // 正确:使用 std::make_shared 函数创建智能指针并初始化
```
这样就能够避免这个错误了。
没有与这些操作数匹配的 "=" 运算符C/C++(349) listener_str.cpp(12, 23): 操作数类型为: std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> = std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>
这个错误通常是因为尝试将一个类型为`std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>`的变量赋值给一个类型为`std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>`的变量,这两个类型虽然都是指向`rclcpp::Subscription`的`shared_ptr`,但是模板参数不同,无法直接赋值。
解决方法是将两个类型匹配,可以通过使用`std::static_pointer_cast`将其中一个类型转换成另一个类型,例如:
```
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> sub;
std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>> sub_const;
// 将 sub_const 转换成 sub 的类型
sub = std::static_pointer_cast<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>(sub_const);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)