???C++11??????????????10?????
???????????? ???????[ 2016/8/24 11:38:14 ] ??????????? C++ .NET
????????#6??????shared_ptr?????????
????????????shared_ptr.get()???api?????shared_ptr???????????????????????????e??????t??????????????????????????δ???
????void StartJob()
????{
????shared_ptr<aircraft> pAircraft(new Aircraft("F-16"));
????Aircraft* myAircraft = pAircraft.get(); // returns the raw pointer
????delete myAircraft; // myAircraft is gone
????}
????</aircraft>
???????????????????????л??????????????myAircraft??????????????????????????????????????????????pAircraft???????????????????????myAircraft???????????????????????????????????????????ACCESS VIOLATION????????????
???????? – ????????????л???????????????????????????????????????????????????delete??????????????????????????????(shared_ptr)?????Access Violate???????????????
????????#7??????????shared_ptr????????????????????????????????
??????????????δ???
????void StartJob()
????{
????shared_ptr<aircraft> ppAircraft(new Aircraft[3]);
????}
????</aircraft>
?????????????????????Aircraft[0] —— Aircraft[1]??Aircraft[2]??????????????????????δ?????????????й?????????????Visual Studio 2015????????????heap corruption???????
???????? – ????????shared_ptr?????????????????????????????????????????????δ???????????????
????void StartJob()
????{
????shared_ptr<aircraft> ppAircraft(new Aircraft[3]?? [](Aircraft* p) {delete[] p; });
????}
????</aircraft>
????????#8??????ù??????????????????
????????????£?????????????shared_ptr????????п?????????????á????????3????????????????????Aircraft?????????Maverick??????????????Iceman??????????????????????????????TopGun????????????Maverick??Iceman???????????Wingman?????????????
?????????????????????Aircraft???????????????????shared_ptr??
????class Aircraft
????{
????private:
????string m_model;
????public:
????int m_flyCount;
????shared_ptr<Aircraft> myWingMan;
????….
?????????main()?????У?????Aircraft?????Maverick??Goose?????????????????????wingman??
????int main()
????{
????shared_ptr<aircraft> pMaverick = make_shared<aircraft>("Maverick: F-14");
????shared_ptr<aircraft> pIceman = make_shared<aircraft>("Iceman: F-14");
????pMaverick->myWingMan = pIceman; // So far so good - no cycles yet
????pIceman->myWingMan = pMaverick; // now we got a cycle - neither maverick nor goose will ever be destroyed
????return 0;
????}
????</aircraft>
??????main()?????????????????????????????????????????——???????????????????????????????????????????????á???????????????????????????????????????????????????ü????????0???????????????????????????
????????????γ??????е?????????
????Aircraft type Maverick: F-14 is created
????Aircraft type Iceman: F-14 is created
????????????????????Bug???????????滻Aircraft???е?shared_ptr?weak_ptr?????????????main()??????????е?????????
????Aircraft type Maverick: F-14 is created
????Aircraft type Iceman: F-14 is created
????Aircraft type Iceman: F-14 is destroyed
????Aircraft type Maverick: F-14 is destroyed
??????????????????Aircraft????????
???????? – ??????????????????????????????????????????????????????????????????????weak_ptr????shared_ptr??
????????#9???????????unique_ptr.release()??????????
????Release()????????????unique_ptr??????????????Release??unique_ptr????????????????н????????????????????????????????????
??????????δ??????????й??????Aircraft?????????????main()????????
????int main()
????{
????unique_ptr<aircraft> myAircraft = make_unique<aircraft>("F-22");
????Aircraft* rawPtr = myAircraft.release();
????return 0;
????}
????</aircraft>
???????? – ???????????unique_ptr???Release()???????????????????????????????????????unique_ptr??????????????unique_ptr.reset()??????
????????#10???????weak_ptr.lock()???????????????Ч???
?????????weak_ptr?????????????lock()????????????weak_ptr??lock()???????????????weak_ptr????????shared_ptr????????????????shared_ptr??????????????????weak_ptr???????shared_ptr????????Ч????????weak_ptr?????????????Ч??weak_ptr?????κε?????????ACESS VIOLATION????????????
????????????????????δ????У????“myWingMan”??weak_ptr???????shared_ptr???????pIceman.reset()????????????????????????weak_ptr????κβ???????????????????
int main()
{
shared_ptr<aircraft> pMaverick = make_shared<aircraft>("F-22");
shared_ptr<aircraft> pIceman = make_shared<aircraft>("F-14");
pMaverick->myWingMan = pIceman;
pIceman->m_flyCount = 17;
pIceman.reset(); // destroy the object managed by pIceman
cout << pMaverick->myWingMan.lock()->m_flyCount << endl; // <span style="color: #ff0000;">ACCESS VIOLATION</span>
return 0;
}
</aircraft>
?????????????????????????????myWingMan???weak_ptr???????????Ч??????????
????if (!pMaverick->myWingMan.expired())
????{
????cout << pMaverick->myWingMan.lock()->m_flyCount << endl;
????}
????У???????????????????????δ???????????????????? – ???99%?????????????????weak_ptr?????????????Ч????????lock????????Ч???????л?????????????????????????Manuel Freiholz???????????????????shared_ptr????????lock()??????????????shared_ptr???????
????shared_ptr<aircraft> wingMan = pMaverick->myWingMan.lock();
????if (wingMan)
????{
????cout << wingMan->m_flyCount << endl;
????}
???????? – ???????weak_ptr?????Ч — ?????????ù?????????????lock()?????????????????
??????????????????????
????????????????????C++11??????????????C++11????????????????????????Щ?顣
????1. C++ Primer (5th Edition) by Stanley Lippman (???????C++ Primer??????棩???????Stanley Lippman)
????2. Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14 by Scott Meyers (???????C++??????????42??????C++11??C++14?÷????????????Scott Meyers)
??????????????C++11?????????????????????????????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??C++????????????C++ lvalue??rvalueC++11????????C++???????????????C++?е?????????????????C++?????????C++???Windows????λ??C/C++???????????????????JAVA??C??C++??????????c++??python???????????????????????????????C++???????C++?е????????C++????????????????C++ ???????????????C++?????????????????????C++????????????
???·???
??????????????????
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????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????