C++????????÷????
???????????? ???????[ 2014/9/19 13:39:18 ] ??????????????? Net C++
??????????
???????? 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 ????(???????????)???????????±?????
??????
???·???
??????????????????
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