C++11????????
??????軨??? ???????[ 2017/5/15 9:51:20 ] ??????????????? ???????????
???????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????????????Ч?????????????????????????÷???
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11