unique_ptr作为类成员并在vector中传递的问题
如下,我把shared_ptr改成unique_ptr,就不能直接在vector中push_back了,C++11新手,这里该怎么改正?
#include <iostream>
#include <string>
#include <memory>
#include <vector>
using namespace std;
class Person
{
public:
Person(string name) : pName(new string(name)){}
~Person(){}
void printName() { cout << *pName << endl; }
private:
unique_ptr<string> pName;
//shared_ptr<string> pName;
};
int main()
{
vector<Person> persons;
Person p("George");
persons.push_back(p);
persons.front().printName();
cout << "==========\n";
return 0;
}
...全文
相关推荐
c++ ppt 课件
**内存管理**:了解动态内存分配(new和delete操作)和智能指针(如unique_ptr、shared_ptr等),防止内存泄漏。12.
自己总结的cc++学习笔记
**智能指针**:如auto_ptr、unique_ptr、shared_ptr等,自动管理对象的生命周期,避免内存泄漏。4.
C++ unique_ptr详解[项目代码]
在实际工程实践中,unique_ptr广泛应用于函数返回值传递场景,调用方无需关心内存释放责任,被调用方亦无需手动delete,极大简化了跨作用域资源传递的代码逻辑。
C++常用术语
智能指针如std::unique_ptr和std::shared_ptr提供了自动内存管理和引用计数,减少了内存泄漏的风险。#### 78.
c++的智能指针Auto_PTR的细致而经典的分析
#### 二、Auto_PTR 简介##### 2.1 动态内存管理问题在 C++ 中,动态内存的分配与释放往往成为开发者需要特别关注的问题。


