C++-Unique_ptr使用

unique_ptr: 一次只能独自享用一个new 出来的对象,多的话会报错。

#include <iostream>
#include <memory>
using namespace std;

class A {
public:
	int i;
	A(int n) :i(n) {};
	~A() {
		cout << "A destructor" << endl;
	}
};

int main()
{	
	unique_ptr<A>up1(new A(2));
	cout << up1->i << endl;
    //unique_ptrup2(up1);
	//cout << up2->i << endl;
	cout << "endl" << endl;
	return 0;
}

你可能感兴趣的:(C++-Unique_ptr使用)