seo優(yōu)化操作淘寶怎么優(yōu)化關(guān)鍵詞步驟
析構(gòu)函數(shù)
析構(gòu)函數(shù)于構(gòu)造函數(shù)相對(duì)應(yīng),構(gòu)造函數(shù)是對(duì)象創(chuàng)建的時(shí)候自動(dòng)調(diào)用的,而析構(gòu)函數(shù)就是對(duì)象在銷(xiāo)毀的時(shí)候自動(dòng)調(diào)用的
特點(diǎn):
1)構(gòu)造函數(shù)可以有多個(gè)來(lái)構(gòu)成重載,但析構(gòu)函數(shù)只能有一個(gè),不能構(gòu)成重載
2)構(gòu)造函數(shù)可以有參數(shù),但析構(gòu)函數(shù)不能有參數(shù)
3)與構(gòu)造函數(shù)相同的是,如果我們沒(méi)有顯式的寫(xiě)出析構(gòu)函數(shù),那么編譯器也會(huì)自動(dòng)的給我們加上一個(gè)析構(gòu)函數(shù),什么都不做;如果我們顯式的寫(xiě)了析構(gòu)函數(shù),那么將會(huì)覆蓋默認(rèn)的析構(gòu)函數(shù)
4)在主函數(shù)中,析構(gòu)函數(shù)的執(zhí)行在return
語(yǔ)句之前,這也說(shuō)明主函數(shù)結(jié)束的標(biāo)志是return
,return
執(zhí)行完后主函數(shù)也就執(zhí)行完了,就算return
后面還有其他的語(yǔ)句,也不會(huì)執(zhí)行的
#include <iostream>
#include <string>
using namespace std;class Cperson
{
public:Cperson(){cout << "Beginning" << endl;}~Cperson(){cout << "End" << endl;}
};int main()
{Cperson op1;system("pause");return 0;
}
運(yùn)行結(jié)果
Beginning
從這里也可以發(fā)現(xiàn),此時(shí)析構(gòu)函數(shù)并沒(méi)有被執(zhí)行,它在system
之后,return
之前執(zhí)行
指針對(duì)象執(zhí)行析構(gòu)函數(shù)
與棧區(qū)普通對(duì)象不同,堆區(qū)指針對(duì)象并不會(huì)自己主動(dòng)執(zhí)行析構(gòu)函數(shù),就算運(yùn)行到主函數(shù)結(jié)束,指針對(duì)象的析構(gòu)函數(shù)也不會(huì)被執(zhí)行,只有使用delete
才會(huì)觸發(fā)析構(gòu)函數(shù)
#include <iostream>
#include <string>
using namespace std;class Cperson
{
public:Cperson(){cout << "Beginning" << endl;}~Cperson(){cout << "End" << endl;}
};int main()
{Cperson *op2 = new Cperson;delete(op2);system("pause");return 0;
}
運(yùn)行結(jié)果
Beginning
End
在這里可以發(fā)現(xiàn),已經(jīng)出現(xiàn)了End
,說(shuō)明析構(gòu)函數(shù)已經(jīng)被執(zhí)行,也就說(shuō)明了delete
觸發(fā)了析構(gòu)函數(shù)
臨時(shí)對(duì)象
格式:類(lèi)名();
作用域只有這一條語(yǔ)句,相當(dāng)于只執(zhí)行了一個(gè)構(gòu)造函數(shù)和一個(gè)析構(gòu)函數(shù)
除了臨時(shí)對(duì)象,也有臨時(shí)變量,例如語(yǔ)句int(12);
就是一個(gè)臨時(shí)變量,當(dāng)這句語(yǔ)句執(zhí)行完了,變量也就釋放了,對(duì)外部沒(méi)有任何影響,我們可以通過(guò)一個(gè)變量來(lái)接受這一個(gè)臨時(shí)的變量,例如:int a=int(12);
這與int a=12;
不同,后者是直接將一個(gè)整型數(shù)值賦給變量a
,而前者是先創(chuàng)建一個(gè)臨時(shí)的變量,然后再將這個(gè)變量賦給變量a
#include <iostream>
#include <string>
using namespace std;class Cperson
{
public:Cperson(){cout << "Beginning" << endl;}~Cperson(){cout << "End" << endl;}
};int main()
{Cperson();system("pause");return 0;
}
運(yùn)行結(jié)果
Beginning
End
析構(gòu)函數(shù)的作用
當(dāng)我們?cè)陬?lèi)中聲明了一些指針變量時(shí),我們一般就在析構(gòu)函數(shù)中進(jìn)行釋放空間,因?yàn)橄到y(tǒng)并不會(huì)釋放指針變量指向的空間,我們需要自己來(lái)delete
,而一般這個(gè)delete
就放在析構(gòu)函數(shù)里面
#include <iostream>
#include <string>
using namespace std;class Cperson
{
public:Cperson(){pp = new int;cout << "Beginning" << endl;}~Cperson(){delete pp;cout << "End" << endl;}private:int *pp;
};int main()
{Cperson();system("pause");return 0;
}
malloc、free和new、delete的區(qū)別
malloc
不會(huì)觸發(fā)構(gòu)造函數(shù),但new
可以
free
不會(huì)觸發(fā)析構(gòu)函數(shù),但delete
可以
#include <iostream>
#include <string>
using namespace std;class Cperson
{
public:Cperson(){pp = new int;cout << "Beginning" << endl;}~Cperson(){delete pp;cout << "End" << endl;}private:int *pp;
};int main()
{Cperson *op1 = (Cperson *)malloc(sizeof(Cperson));free(op1);Cperson *op2 = new Cperson;delete op2;system("pause");return 0;
}
運(yùn)行結(jié)果
Beginning
End
從結(jié)果上來(lái)看,只得到了一組Beginning、End
說(shuō)明只有一組觸發(fā)了構(gòu)造函數(shù)和析構(gòu)函數(shù),這一組就是new和delete