???????c++??????c++???????????????????????????????????ò??????3???????????????????????????????????????Щ???????????????????ɡ?
????1 ???????????????????????????????????????????????????????????????????????????п???????????????汻???·???????????????????????????
????2 ???????????????????????????浥??????????????????·????????浥????????????????
????3 ???й????????????浥????????????????????????????????????3??????????????????
?????????????????????棬????????????????????????????????Ч????????????棬?????????????????????????????????????????????????????????????c++11??????????????????????????????????????‘С????????’??????
?????????????C/C++?????????????????????????м?????????????????????????д???????????????????
????c++11?????unique_ptr??shared_ptr??weak_ptr???????????????????????檔
#include <memory>
#include <iostream>
using namespace std;
struct Test{
int a;
};
class Demo {
public:
Demo() {
b = new Test();
std::cout << "Demo ()" << std::endl;
}
~Demo() {
std::cout << "~Demo ()" << std::endl;
}
void Say() {
std::cout << "Dem::Say()==>" << b->a << std::endl;
}
public:
Test *b;
};
int main()
{
std::cout << "////////////////unique_ptr///////////////////////" << std::endl;
////////////////unique_ptr///////////////////////
unique_ptr<Demo> unique_ptr_demo1(new Demo);
unique_ptr_demo1->Say();
// unique_ptr<Demo> unique_ptr_demo2 = unique_ptr_demo1; //??????????
Demo demo2 = *unique_ptr_demo1;
unique_ptr<Demo> unique_ptr_demo3 = move(unique_ptr_demo1);
unique_ptr_demo3->Say();
// Demo demo5 = *unique_ptr_demo1; //?????????
//
////////////////shared_ptr///////////////////////
std::cout << "////////////////shared_ptr///////////////////////" << std::endl;
shared_ptr<Demo> shared_ptr_demo1(new Demo());
shared_ptr_demo1->Say();
// shared_ptr_demo1.reset();
shared_ptr<Demo> shared_ptr_demo2 = shared_ptr_demo1;
shared_ptr_demo1.reset();
// shared_ptr_demo2.reset(); //?????????2
shared_ptr_demo2->Say();
return 0;
}

?????????????????????????????????????????棬???????????????????棬?????????????????ε?????????unique_ptr_demo3??demo2??shared_ptr_demo1.
?????????????unique_ptr??shared_ptr??????unique_ptr?????????????????????????????????????????????棨?????У??????????????????????unique_ptr???????????unique_ptr???????????????unique_ptr???????????????????????????????????????????????move?????????unique_ptr????棬???????????????????????????????????????????????????????????????????????????????????????????????????????????????
????shared_ptr??????????????shared_ptr?????????棬???????????ü????????????????????????????????????????????棬???????????????????????????е????ü??????0?????????檔???????????????? shared_ptr_demo1.reset();????????shared_ptr_demo2?????shared_ptr_demo1.reset????????????????棬shared_ptr_demo2????????????????????????reset??????????????????????ü???????0???????????ó?nullptr??????????????????????????棬???????????????????????????reset????????????????
#include <memory>
#include <iostream>
using namespace std;
class Demo{
public:
void Say() {
std::cout << "Demo" << std::endl;
}
};
template <class T>
void CheckPtrIsValid(weak_ptr<T>& temp) {
shared_ptr<T> demo = temp.lock();
if (demo != nullptr) {
std::cout << "ptr is good" << std::endl;
}else{
std::cout << "ptr is null" << std::endl;
}
}
int main()
{
shared_ptr<Demo> shared_ptr_demo1(new Demo());
shared_ptr<Demo> shared_ptr_demo2 = shared_ptr_demo1;
weak_ptr<Demo> weak_ptr_demo1 = shared_ptr_demo1;
CheckPtrIsValid(weak_ptr_demo1);
shared_ptr_demo1->Say();
shared_ptr_demo2->Say();
shared_ptr_demo1.reset();
CheckPtrIsValid(weak_ptr_demo1);
shared_ptr_demo2.reset();
CheckPtrIsValid(weak_ptr_demo1);
return 0;
}

?????????weak_ptr?????????ò??????????????????????????????shared_ptr??????棬??????и???棬??????lock???????????????shared_ptr?????????????????????????????Ч??????????????????????????ε???reset???????????????ü??????0???????檔??????2?Σ??????????η?????????????weak_ptr????????????ü????????????????и???檔
???????????????????о?????????????????shared_ptr????????????????????????????????????unique _ptr????weak_ptr???????????????????shared_ptr????????????Ч?????????????????????????÷???