??????????
???????? C++ ???????????????????????????? new ????????涼???? delete??????????? delete???????????????????? delete???????3????????????????? delete ???????????????
????????????????????Ч???????????????????????μ????????????÷?????????std::auto_ptr??boost::scoped_ptr??boost::shared_ptr??boost::scoped_array??boost::shared_array??boost::weak_ptr??boost:: intrusive_ptr???????????????????????????new??delete???????????б??????????????o?????????????????д???
???????水??????????? 7 ?????????smart_ptr????
???????????????
????1??????
????????????????????????????????????????????????????????????????????????????????????????????????????????????檔?????????????????“operator->”??????????????????????????????????????????????????????????“.”????????
???????????????????????????????? get() ??????????????????????????????if (my_smart_object)?????棬??ж?????????????????????????????ж??if (my_smart_object.get())??
???????????????? reset() ??????????????????????????? NULL??????????????????????????檔????????????????????????????????????????′???????
?????????д?????????????????????
class Simple {
public:
Simple(int param = 0) {
number = param;
std::cout << "Simple: " << number << std::endl;
}
~Simple() {
std::cout << "~Simple: " << number << std::endl;
}
void PrintSomething() {
std::cout << "PrintSomething: " << info_extend.c_str() << std::endl;
}
std::string info_extend;
int number;
};
????2??std::auto_ptr
????std::auto_ptr ???? STL??????? namespace std ?У????????? #include<memory> ???????á?std::auto_ptr ??????????????????????
???????????????????
void TestAutoPtr() {
std::auto_ptr<Simple> my_memory(new Simple(1));   // ?????????????Simple??1
if (my_memory.get()) {                            // ?ж??????????????
my_memory->PrintSomething();                    // ??? operator-> ???????????????е????
my_memory.get()->info_extend = "Addition";      // ??? get() ?????????????????????
my_memory->PrintSomething();                    // ??δ??????????????????
(*my_memory).info_extend += " other";           // ??? operator* ???????????????????????“.”???????????????е????
my_memory->PrintSomething();                    // ??δ??????????????????
}
}
                                                // my_memory ??????????????????????????? Simple(1)
??????н?????
????Simple: 1
????PrintSomething:
????PrintSomething: Addition
????PrintSomething: Addition other
????~Simple: 1
???????????????? std::auto_ptr ????????????????????????β?????????????????? delete ???
????????t?????????????????μ???????????
void TestAutoPtr2() {
std::auto_ptr<Simple> my_memory(new Simple(1));
if (my_memory.get()) {
std::auto_ptr<Simple> my_memory2;   // ????????μ? my_memory2 ????
my_memory2 = my_memory;             // ?????? my_memory ?? my_memory2
my_memory2->PrintSomething();       // ??????????????
my_memory->PrintSomething();        // ????
}
}
??????????????±????????????????? C++ ?????????????????????? std::auto_ptr ????????????????????????“my_memory2 = my_memory”?????д???my_memory2 ???????? my_memory ??????????????????? my_memory ????(???????????)???????????±?????