???????????? std::auto_ptr ??????????“operator=”???????????????????????????????????????[1]???????????械????????
???????? std::auto_ptr ?t?????????????????????????????????
????void TestAutoPtr3() {
????std::auto_ptr<Simple> my_memory(new Simple(1));
????if (my_memory.get()) {
????my_memory.release();
????}
????}
??????薪?????
????Simple: 1
??????????????????????????????????斜?????????????“~Simple: 1”?????????泄?????????????? my_memory ???????????????????? release() ?????????妫�???????????泄???????????????校????my_memory????????妫�????????????????????榛�?????????? my_memory ????????????榛�????
??????????????????
void TestAutoPtr3() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
Simple* temp_memory = my_memory.release();
delete temp_memory;
}
}
??
void TestAutoPtr3() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory.reset();  // ??? my_memory ???????????
}
}
??????? std::auto_ptr ?? release() ????????贸??????????????????????? C++ ??????
???????std::auto_ptr ?????????????????????妫�?????????????????
??????1??    ??????????“operator=”??????????????????????????
??????2??    ??? release() ???????????????????榛�???????
??????3??    std::auto_ptr ?貌?????????????????????????写?????????????????
??????4??    ???? std::auto_ptr ??“operator=”???????????????????? std::vector ???????小?
??????5??    ……
?????????? std::auto_ptr ???????????????????????????????椋�???????????????????????????????????????????????小??????????????
???????? std::auto_ptr ??????????????些?????????????? C++ ???????????????????? boost ?????????boost ????????????????????
????????????????????
????3??boost::scoped_ptr
????boost::scoped_ptr ???? boost ???????? namespace boost ?校????????? #include<boost/smart_ptr.hpp> ???????谩?boost::scoped_ptr ?? std::auto_ptr ??????????????????????????????????boost::scoped_ptr ????????????????? std::auto_ptr ????????????
??????????????????????
void TestScopedPtr() {
boost::scoped_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory->PrintSomething();
my_memory.get()->info_extend = "Addition";
my_memory->PrintSomething();
(*my_memory).info_extend += " other";
my_memory->PrintSomething();
my_memory.release();           // ???? error: scoped_ptr ??? release ????
std::auto_ptr<Simple> my_memory2;
my_memory2 = my_memory;        // ???? error: scoped_ptr ??????? operator=????????????????
}
}
?????????????????????boost::scoped_ptr ??????? auto_ptr ?????????谩???????? release() ????????????????????泄???????危????? boost::scoped_ptr ??????????????????????????写“my_memory2 = my_memory”?????????????? std::auto_ptr ?????????????
???????? boost::scoped_ptr ???????????????????????????????????????????????????????????????????????????????????????????????????????????????渭? boost::shared_ptr??
????4??boost::shared_ptr
????boost::shared_ptr ???? boost ???????? namespace boost ?校????????? #include<boost/smart_ptr.hpp> ???????谩?????????????? boost::scoped_ptr ???????????????????????????boost::shared_ptr ???????????????????????????????????????????????????眉?????boost::shared_ptr ???????????????????????
??????????????????????
void TestSharedPtr(boost::shared_ptr<Simple> memory) {  // ?????????? reference (?? const reference)
memory->PrintSomething();
std::cout << "TestSharedPtr UseCount: " << memory.use_count() << std::endl;
}
void TestSharedPtr2() {
boost::shared_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
my_memory->PrintSomething();
my_memory.get()->info_extend = "Addition";
my_memory->PrintSomething();
(*my_memory).info_extend += " other";
my_memory->PrintSomething();
}
std::cout << "TestSharedPtr2 UseCount: " << my_memory.use_count() << std::endl;
TestSharedPtr(my_memory);
std::cout << "TestSharedPtr2 UseCount: " << my_memory.use_count() << std::endl;
//my_memory.release();// ???? error: ?????shared_ptr ???? release ????
}
??????薪?????
????Simple: 1
????PrintSomething:
????PrintSomething: Addition
????PrintSomething: Addition other
????TestSharedPtr2 UseCount: 1
????PrintSomething: Addition other
????TestSharedPtr UseCount: 2
????TestSharedPtr2 UseCount: 1
????~Simple: 1
????boost::shared_ptr ????????????谩???????? release() ??????????????boost::shared_ptr ??????????????眉????????????????????????????boost::shared_ptr ??????????? use_count() ??????????? boost::shared_ptr ????????眉?????????薪???????????????? TestSharedPtr2 ?????校????眉???? 1??????????????????????胃???????????TestSharedPtr ????????眉????2???? TestSharedPtr ????????眉????????? 1????????????????????????????boost::shared_ptr ????貌????????
??????????????????????????????????????????????????????椋�???????????????
????5??boost::scoped_array
????boost::scoped_array ???? boost ???????? namespace boost ?校????????? #include<boost/smart_ptr.hpp> ???????谩?
????boost::scoped_array ????????????????????? boost::scoped_ptr ???????????????????
??????????????????????
void TestScopedArray() {
boost::scoped_array<Simple> my_memory(new Simple[2]); // ?????????????????
if (my_memory.get()) {
my_memory[0].PrintSomething();
my_memory.get()[0].info_extend = "Addition";
my_memory[0].PrintSomething();
(*my_memory)[0].info_extend += " other";            // ???? error??scoped_ptr ??????? operator*
my_memory[0].release();                             // ??????? release ????
boost::scoped_array<Simple> my_memory2;
my_memory2 = my_memory;                             // ???? error????????????? operator=
}
}
????boost::scoped_array ?????? boost::scoped_ptr ?????????????????????????????????????椤�????boost::scoped_array ???????“operator*”???????????????????锟�???????? get() ?????????些??
????????????y? boost::shared_array ???????????眉?????????????????????????????
????6??boost::shared_array
????boost::shared_array ???? boost ???????? namespace boost ?校????????? #include<boost/smart_ptr.hpp> ???????谩?
???????? boost::scoped_array ??????????????????????锟�??????????????????????????????????????? boost::shared_array???? boost::shared_ptr ????????????????眉?????
??????????????????????
void TestSharedArray(boost::shared_array<Simple> memory) {  // ?????????? reference (?? const reference)
std::cout << "TestSharedArray UseCount: " << memory.use_count() << std::endl;
}
void TestSharedArray2() {
boost::shared_array<Simple> my_memory(new Simple[2]);
if (my_memory.get()) {
my_memory[0].PrintSomething();
my_memory.get()[0].info_extend = "Addition 00";
my_memory[0].PrintSomething();
my_memory[1].PrintSomething();
my_memory.get()[1].info_extend = "Addition 11";
my_memory[1].PrintSomething();
//(*my_memory)[0].info_extend += " other";  // ???? error??scoped_ptr ??????? operator*
}
std::cout << "TestSharedArray2 UseCount: " << my_memory.use_count() << std::endl;
TestSharedArray(my_memory);
std::cout << "TestSharedArray2 UseCount: " << my_memory.use_count() << std::endl;
}
??????薪?????
????Simple: 0
????Simple: 0
????PrintSomething:
????PrintSomething: Addition 00
????PrintSomething:
????PrintSomething: Addition 11
????TestSharedArray2 UseCount: 1
????TestSharedArray UseCount: 2
????TestSharedArray2 UseCount: 1
????~Simple: 0
????~Simple: 0
?????? boost::shared_ptr ?????????????眉???????????????????????????
??????????????????????????? std::auto_ptr??boost::scoped_ptr??boost::shared_ptr??boost::scoped_array??boost::shared_array????????????????????????????????90% ????霉????????????????? 5 ????????????????????????????????????????????????????伞?