构造函数带有参数如何使用shared_ptr
时间: 2024-02-01 20:02:22 浏览: 32
在使用 `shared_ptr` 时,我们可以使用 `make_shared` 函数来创建一个对象并返回一个指向该对象的 `shared_ptr` 智能指针。如果构造函数带有参数,我们可以将这些参数作为 `make_shared` 函数的参数传递进去。例如:
```c++
#include <memory>
#include <iostream>
class MyClass {
public:
MyClass(int a, int b) : m_a(a), m_b(b) {}
void print() {
std::cout << "a: " << m_a << ", b: " << m_b << std::endl;
}
private:
int m_a;
int m_b;
};
int main() {
std::shared_ptr<MyClass> ptr = std::make_shared<MyClass>(1, 2);
ptr->print();
return 0;
}
```
在上面的例子中,我们定义了一个带有两个参数的构造函数 `MyClass(int a, int b)`,然后使用 `make_shared` 函数创建了一个 `MyClass` 类型的对象,并将参数 1 和 2 传递给构造函数。最后,我们将返回的 `shared_ptr` 智能指针赋值给 `ptr` 变量,并通过 `ptr` 调用了 `print` 函数来输出对象的成员变量。
相关问题
函数返回shared_ptr函数返回shared_ptr
在C++中,函数可以返回一个shared_ptr对象。可以通过以下几种方式来返回shared_ptr对象:
1. 使用make_shared函数:可以使用std::make_shared函数来创建一个shared_ptr对象,并将其作为函数的返回值。例如:
```cpp
std::shared_ptr<MyClass> createObject() {
return std::make_shared<MyClass>("Hello", 3.14);
}
```
2. 使用new关键字:可以使用new关键字手动创建一个对象,并将其包装在shared_ptr中返回。例如:
```cpp
std::shared_ptr<int> createInt() {
return std::shared_ptr<int>(new int(1));
}
```
3. 使用reset函数:可以先创建一个空的shared_ptr对象,然后使用reset函数将其重新指向一个新的对象。例如:
```cpp
std::shared_ptr<int> createInt() {
std::shared_ptr<int> ptr;
ptr.reset(new int(1));
return ptr;
}
```
无论使用哪种方式,函数返回的shared_ptr对象都可以在函数外部进行使用和管理。
#### 引用[.reference_title]
- *1* *3* [C++11 解决内存泄露问题的智能指针:shared_ptr、unique_ptr、weak_ptr](https://blog.csdn.net/weixin_44120785/article/details/128714630)[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* [智能指针shared_ptr、unique_ptr、weak_ptr](https://blog.csdn.net/weixin_44477424/article/details/125902769)[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 ]
shared_ptr的构造函数
`std::shared_ptr` 的构造函数有多个重载形式,其中最基本的一个形式是:
```c++
template< class T >
explicit shared_ptr( T* ptr );
```
这个构造函数用于创建一个指向类型为 `T` 的对象的 `shared_ptr`,并将其管理这个对象的内存。它接受一个指向类型 `T` 的指针 `ptr`,并将其存储在内部,以便在 `shared_ptr` 的整个生命周期中管理这个指针所指向的对象的内存。
除此之外,`std::shared_ptr` 还支持其他多种构造函数重载形式,例如:
- 接受自定义删除器的构造函数
- 接受一个已存在的 `shared_ptr` 对象的构造函数
- 接受一个已存在的 `std::weak_ptr` 对象的构造函数等等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)