???????????μ?C++11???????????????????????????????????????????????????????????C++11??????????????C++????????????
?????????????????C++11???????????????з????????????C++11???????????????Щ??????????Ч???????????????????????????????????????????????
?????????????????????????Aircraft???????????Щ????
class Aircraft
{
private:
string m_model;
public:
int m_flyCount;
weak_ptr<aircraft> myWingMan;
void Fly()
{
cout << "Aircraft type" << m_model << "is flying !" << endl;
}
Aircraft(string model)
{
m_model = model;
cout << "Aircraft type " << model << " is created" << endl;
}
Aircraft()
{
m_model = "Generic Model";
cout << "Generic Model Aircraft created." << endl;
}
~Aircraft()
{
cout << "Aircraft type " << m_model << " is destroyed" << endl;
}
};
</aircraft>
????????#1??????????????????????
??????????????е?????????й???????????????shared_ptr????????????????????????????е?????????????Щ??????????90%??????У???shared_ptr???????????????????
?????????????????????????????
????1??????????????????е???????????????shared_ptr??????unique_ptr???????????????????й????Щbug??
????????????bug????????????????????????????????????????????????????????????????????????????/?????????????????????????飡
?????????????????????????????????????????????????????????????????????棬???????????????shared_ptr?????÷?Χ??
????2??????shared_ptr?????unique_ptr????????????
????shared_ptr??????????????????????????????ü????????????????????飬???????unique_ptr????????
???????? – ???????£?????????unique_ptr????????????й???????????????????????????????????????shared_ptr??
????????#2????б??shared_ptr????????/?????????????
????Shared_ptr??????????????????????????????Щ?????????????????????Щ???????????????shared_ptr?????????????????????????????????????????????????Щ?????????????shared_ptr?????????????????????
????????– ???????д???????????????????????????????????unique_ptr??
????????#3?????auto_ptr??
????auto_ptr????????Σ?????????????????????????????????????????????????????????????????????????????auto????????????????????????????????????????????:
????int main()
????{
????auto_ptr<aircraft> myAutoPtr(new Aircraft("F-15"));
????SetFlightCountWithAutoPtr(myAutoPtr); // Invokes the copy constructor for the auto_ptr
????myAutoPtr->m_flyCount = 10; // <span style="color: #ff0000;">CRASH !!!</span>
????}
????</aircraft>
???????? – unique_ptr???????auto_ptr?????й?????????????????????????????????????auto_ptr???????????滻??unique_ptr????????????2????????????
????????#4????????make_shared???????shared_ptr??
?????????????????make_share??????????????
????1.????: ??????new?????????????????????shared_ptr?????????????ζ????????棺?????????new?????????????????????????shared_ptr?????????????????????????????
????shared_ptr<aircraft> pAircraft(new Aircraft("F-16")); // Two Dynamic Memory allocations - SLOW !!!
????</aircraft>
??????????????????make_shared?????C++???????????????????????????棬???????????????????????????????
????shared_ptr<aircraft> pAircraft = make_shared<aircraft>("F-16"); // Single allocation - FAST !
????</aircraft></aircraft>
????2???????MS????????memory??????????????????????????????????????????????????????????????????????????????
????????- ???make_shared?????????????????????????
????????#5???????????????????????????????????shared_ptr??
????????????????????????????????????shared_ptr?????????????????????á?
????????????????????
int main()
{
Aircraft* myAircraft = new Aircraft("F-16");
shared_ptr<aircraft> pAircraft(myAircraft);
cout << pAircraft.use_count() << endl; // ref-count is 1
shared_ptr<aircraft> pAircraft2(myAircraft);
cout << pAircraft2.use_count() << endl; // ref-count is 1
return 0;
}
</aircraft>
???????????ACCESS VIOLATION??????????????????????3????????????
????????????????????????shared_ptr?????????????myAircraft?????????????????shared_ptr????????????????????γ?????????????????????????
????????– ????????make_shared????shared_ptr?????????????????δ???????????????????????????
????shared_ptr<aircraft> pAircraft(new Aircraft("F-16"));
????</aircraft>