C++自動(dòng)析構(gòu)時(shí)的順序問題
自動(dòng)析構(gòu)時(shí)是先析構(gòu)后構(gòu)造的.
//普通(非模板)類的成員模板 class DebugDelete{ public: DebugDelete(ostream &s = cerr) :os(s){} template <typename T>void operator()(T*p)const { os << "deleting unique_ptr " <<typeid(T).name() <<endl; delete p; } private: ostream &os; }; void demo_general_class_tempalte_member() { double *p = new double; DebugDelete d; d(p);//d調(diào)用DebugDelet::operator()(double*),釋放p int* ip = new int; //在一個(gè)臨時(shí)DebugDelete 對(duì)象上調(diào)用operator()(int*) DebugDelete()(ip); //實(shí)例化DebugDelete::opeartor()<int>(int*)const unique_ptr<int, DebugDelete>p2(new int, DebugDelete()); //實(shí)例化DebugDelete::opeartor()<string>(string*)const unique_ptr<string, DebugDelete>sp(new string, DebugDelete()); }
這里輸出
deleting unique_ptr double
deleting unique_ptr int
deleting unique_ptr class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
deleting unique_ptr int
可以看到,主動(dòng)析構(gòu)的正常進(jìn)行.
函數(shù)結(jié)束后自動(dòng)析構(gòu)的,先創(chuàng)建了int后創(chuàng)建了string,但是先析構(gòu)了string
class B { public: virtual ~B(){ cout << "delete B" << endl; } }; class D :B { public: virtual ~D() override{ cout << "delete D" << endl; } }; void demo_delete() { D d; }
輸出
delete D
delete B
這里構(gòu)造時(shí)是先構(gòu)造基類,再構(gòu)造派生類.但是在析構(gòu)時(shí)是先析構(gòu)了子類,再析構(gòu)了基類。
知識(shí)點(diǎn)補(bǔ)充:C++ 構(gòu)造與析構(gòu)的執(zhí)行順序
1、代碼如下:
class A { public: int _Id; A():_Id(0) { printf("A[%d]\n",_Id); } ~A() { printf("~A[%d]\n",_Id); } }; class B { public: A _A; A* _PA; B() { printf("B\n"); } ~B() { printf("~B\n"); delete _PA; } }; int main(int argc, char* argv[]) { { B b; b._PA = new A(); b._PA->_Id = 17; } return 0; }
2、執(zhí)行順序
A[0]
B
A[0]
~B
~A[17]
~A[0]
3、B是棧上對(duì)象,C++保證棧上對(duì)象離開作用域,會(huì)自動(dòng)調(diào)用析構(gòu)方法。
4、考慮b中的對(duì)象,_A是棧上對(duì)象,_PA是指針,堆上對(duì)象,對(duì)于_PA必須delete,否則資源泄露。而對(duì)于_A不需要處理,會(huì)自動(dòng)調(diào)用析構(gòu)方法??梢赃@樣理解,對(duì)象b離開作用域,調(diào)用析構(gòu)方法,而b中的_A當(dāng)然也離開了作用域(皮之不存毛將存焉),調(diào)用析構(gòu)方法。
5、碰到過這樣的情況,vs自動(dòng)生成的析構(gòu)方法有問題,導(dǎo)致崩潰。手動(dòng)添加一個(gè)析構(gòu)方法,就可以了。
總結(jié)
到此這篇關(guān)于C++自動(dòng)析構(gòu)時(shí)的順序的文章就介紹到這了,更多相關(guān)C++自動(dòng)析構(gòu)時(shí)的順序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從零學(xué)習(xí)構(gòu)造系統(tǒng)之bazel示例詳解
這篇文章主要為大家介紹了從零學(xué)習(xí)構(gòu)造系統(tǒng)之bazel示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02C++實(shí)現(xiàn)洗牌發(fā)牌排序功能的示例代碼
本篇文章主要介紹了C++實(shí)現(xiàn)洗牌發(fā)牌排序功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10詳解C++標(biāo)準(zhǔn)庫中處理正則表達(dá)式的類std::regex
std?是?C++?標(biāo)準(zhǔn)庫的命名空間,包含了大量標(biāo)準(zhǔn)的?C++?類、函數(shù)和對(duì)象,這些類和函數(shù)提供了廣泛的功能,包括輸入輸出、容器、算法、字符串處理等,這篇文章主要介紹了C++標(biāo)準(zhǔn)庫中提供的用于處理正則表達(dá)式的類std::regex,需要的朋友可以參考下2024-03-03C語言的進(jìn)制轉(zhuǎn)換及算法實(shí)現(xiàn)教程
這篇文章主要介紹了C語言的進(jìn)制轉(zhuǎn)換及算法實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01