C++????????????
???????????? ???????[ 2015/1/28 14:08:27 ] ????????C++ .NET ????
????2. C++????????????
????STL????????????????????????auto_ptr??unique_ptr??shared_ptr??weak_ptr??????????????????
???????auto_ptr??C++98????????????C+11???????????????????????????????????????????auto_ptr????????????????????????????????????????????????????????????auto_ptr????????
???????????
???????е???????????????explicit??????????????????????????auto_ptr?????????????
????templet<class T>
????class auto_ptr {
????explicit auto_ptr(X* p = 0) ;
????...
????};
????????????????????????????????????????????
????shared_ptr<double> pd;
????double *p_reg = new double;
????pd = p_reg; // not allowed (implicit conversion)
????pd = shared_ptr<double>(p_reg); // allowed (explicit conversion)
????shared_ptr<double> pshared = p_reg; // not allowed (implicit conversion)
????shared_ptr<double> pshared(p_reg); // allowed (explicit conversion)
?????????????????????????????
????string vacation("I wandered lonely as a cloud.");
????shared_ptr<string> pvac(&vacation); // No
????pvac?????????????delete?????????????棬?????????
??????t???
#include <iostream>
#include <string>
#include <memory>
class report
{
private:
std::string str;
public:
report(const std::string s) : str(s) {
std::cout << "Object created.
";
}
~report() {
std::cout << "Object deleted.
";
}
void comment() const {
std::cout << str << "
";
}
};
int main() {
{
std::auto_ptr<report> ps(new report("using auto ptr"));
ps->comment();
}
{
std::shared_ptr<report> ps(new report("using shared ptr"));
ps->comment();
}
{
std::unique_ptr<report> ps(new report("using unique ptr"));
ps->comment();
}
return 0;
}
????3. ???????auto_ptr??
????????????????????:
????auto_ptr< string> ps (new string ("I reigned lonely as a cloud.”??;
????auto_ptr<string> vocation;
????vocaticn = ps;
?????????????佫???????????????ps??vocation????????????????????????string?????????????????????????????????????????——?????ps??????????????vocation?????????????????????????ж????
??????????????????????????????????????????????????е??????????????????????????????????????????????δ??????????
???????????????ownership???????????????????????????????????У??????????ж????????????????????????????????????????????????????????auto_ptr??uniqiie_ptr ????????unique_ptr?????????
?????????????????????????????????????????????????????ü????????磬??????????????1?????????????????????1?????????0??????delete??????shared_ptr?????????
??????????????????????????????????
????????????????????????????????auto_ptr???
?????????????????????
#include <iostream>
#include <string>
#include <memory>
using namespace std;
int main() {
auto_ptr<string> films[5] =
{
auto_ptr<string> (new string("Fowl Balls"))??
auto_ptr<string> (new string("Duck Walks"))??
auto_ptr<string> (new string("Chicken Runs"))??
auto_ptr<string> (new string("Turkey Errors"))??
auto_ptr<string> (new string("Goose Eggs"))
};
auto_ptr<string> pwin;
pwin = films[2]; // films[2] loses ownership. ?????????films[2]????pwin?????films[2]????????????????????????
cout << "The nominees for best avian baseballl film are
";
for(int i = 0; i < 5; ++i)
cout << *films[i] << endl;
cout << "The winner is " << *pwin << endl;
cin.get();
return 0;
}
?????????·????????????????????????????????????films[2]???????????????????????????????????????????????auto_ptr????shared_ptr??unique_ptr???????????????????£?
???????shared_ptr??????????????shared_ptr???????ü?????pwin??films[2]???????????棬?????????????????ж????ü???????С?????????????????????????
???????unique_ptr???????????auto_ptr?????unique_ptr?????????????????????unique_ptr??????????????н?α?????????????????????????г??????
????unique_ptr<string> pwin;
????pwin = films[2]; // films[2] loses ownership.
?????????????????????
????????????????auto_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