c++11 关于auto_ptr
- 智能指针需要引用头文件 <memory>
- c++ 11不再建议使用auto_ptr,编译时会产生 deprecated(及不赞成) 告警
- auto_ptr的功能可以被unique_ptr完全替换
auto_ptr代码示例
#include <iostream>
#include <memory>
using namespace std;
int main()
{
auto_ptr<string> p1 (new string("point to p1"));
auto_ptr<string> p2 = p1; // 此处赋值不会报错,但是会导致p1悬空
//后续对于p1的引用会崩溃
cout << "p2: " << *p2 << " ; p1: " << *p1 << endl;
}
unique_ptr代码示例
#include <iostream>
#include <memory>
using namespace std;
int main()
{
unique_ptr<string> p1(new string("point to p1"