C++11??????????????
???????????? ???????[ 2016/8/15 10:47:09 ] ??????????? .NET
????????C++11????????????巋????漰???????????????????shared_ptr?? unique_ptr?? weak_ptr)??
????????toy code??????????????????o?????????
????shared_ptr
????shared_ptr ?????÷?
????shared_ptr???????ü????????????????????????????μ?shared_ptr??????????????????shared_ptr????????ü?????1????shared_ptr??????????????ü?????1???????ü????0???????????????檔
???????????????????????????????????????????????????????е???????????????????????????????У??????????????????????????棻?????????????????shared_ptr??????
??????????????make_shared?????shared_ptr??
cout<<"test shared_ptr base usage:"<<endl;
shared_ptr<string> p1 = make_shared<string>("");
if(p1 && p1->empty())
*p1 = "hello";
auto p2 = make_shared<string>("world");
cout<<*p1<<' '<<*p2<<endl;
cout<<"test shared_ptr use_count:"<<endl;
cout<<"p1 cnt:"<<p1.use_count()<<" p2 cnt:"<<p2.use_count()<<endl;
auto p3 = p2;
cout<<"p1 cnt:"<<p1.use_count()<<" p2 cnt:"<<p2.use_count()<<" p3 cnt:"<<p3.use_count()<<endl;
p2 = p1;
cout<<"p1 cnt:"<<p1.use_count()<<" p2 cnt:"<<p2.use_count()<<" p3 cnt:"<<p3.use_count()<<endl;
????shared_ptr ?? new
????shared_ptr??????????new??????????????г??????
????cout<<"test shared_ptr and new:"<<endl;
????shared_ptr<int> p4(new int(1024));
????//shared_ptr<int> p5 = new int(1024); // wrong?? no implicit constructor
????cout<<*p4<<endl;
?????????????????new???????????????shared_ptr??
????????????????????????????new??shared_ptr!
void process(shared_ptr<int> ptr)
{
cout<<"in process use_count:"<<ptr.use_count()<<endl;
}
cout<<"don't mix shared_ptr and normal pointer:"<<endl;
shared_ptr<int> p5(new int(1024));
process(p5);
int v5 = *p5;
cout<<"v5: "<<v5<<endl;
int *p6 = new int(1024);
process(shared_ptr<int>(p6));
int v6 = *p6;
cout<<"v6: "<<v6<<endl;
??????????????λ??????
????in process use_count:2
????v5: 1024
????in process use_count:1
????v6: 0
??????????????????process p6???shared_ptr?????ü????1??????process??????????????????????棬???p6????????????
?????????????????new??????????????shared_ptr?????????????????????????????棡
????shared_ptr.reset
????shared_ptr???????reset????????????????????????????????ü????????
????cout<<"test shared_ptr reset:"<<endl;
????cout<<"p1 cnt:"<<p1.use_count()<<" p2 cnt:"<<p2.use_count()<<" p3 nt:"<<p3.use_count()<<endl;
????p1.reset(new string("cpp11"));
????cout<<"p1 cnt:"<<p1.use_count()<<" p2 cnt:"<<p2.use_count()<<" p3 cnt:"<<p3.use_count()<<endl;
????shared_ptr deleter
??????????????deleter????????????shared_ptr??????????á?
????void print_at_delete(int *p)
????{
????cout<<"deleting..."<<p<<' '<<*p<<endl;
????delete p;
????}
????cout<<"test shared_ptr deleter:"<<endl;
????int *p7 = new int(1024);
????shared_ptr<int> p8(p7?? print_at_delete);
????p8 = make_shared<int>(1025);
????unique_ptr
????unique_ptr?????÷?
????unique_ptr???????????????????????????????????????????????unique_ptr???п?????????????????????????release??????unique_ptr????????????
cout<<"test unique_ptr base usage:"<<endl;
unique_ptr<int> up1(new int(1024));
cout<<"up1: "<<*up1<<endl;
unique_ptr<int> up2(up1.release());
cout<<"up2: "<<*up2<<endl;
//unique_ptr<int> up3(up1); // wrong?? unique_ptr can not copy
//up2 = up1; // wrong?? unique_ptr can not copy
unique_ptr<int> up4(new int(1025));
up4.reset(up2.release());
cout<<"up4: "<<*up4<<endl;
??????
???·???
??????????????????
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