1.野指针
2.智能指针unique_ptr
3.共享指针shared_ptr
4.弱指针weak_ptr
#include <iostream>
#include <memory>
using namespace std;
class stu{
public:
stu(){
cout<<"构造函数"<<endl;
}
~stu(){
cout<<"析构函数"<<endl;
}
};
int main() {
int *p0;
int *p = new int(5);
cout << "p="<<*p << endl;
delete p;
cout <<"p=" <<*p << endl;
unique_ptr<int> pun(new int(3));
cout << "pun="<< *pun << endl;
unique_ptr<int> pun2 = move(pun);
cout << "pun2="<<*pun2<< endl;
pun2.reset();
pun2.reset(new int(5));
int * p2 = pun2.get();
cout << "p2="<< *p2<< endl;
shared_ptr<stu> sh(new stu());
cout << "当前指针的计数="<< sh.use_count() << endl;
shared_ptr<stu> sh2 = sh;
sh.reset();
sh2.reset();
int *pr = new int(10);
shared_ptr<int> spa(pr);
cout<< "sp="<< spa.use_count()<<endl;
weak_ptr<int> wp(spa);
cout<< wp.use_count()<<endl;
shared_ptr<int> sp03 = wp.lock();
return 0;
}