Java对象析构_c++之对象构造顺序和销毁(析构函数)
一、對(duì)象的構(gòu)造順序:
1、對(duì)于局部對(duì)象:
當(dāng)程序執(zhí)行流到達(dá)對(duì)象的定義語(yǔ)句時(shí)進(jìn)行構(gòu)造。下面還是用代碼來(lái)解析這句話:
#include?
class?Test
{
private:
int?mi;
public:
Test(int?i)
{
mi=i;
printf("Test(int?i)?is?%d\n",mi);
}
Test(const?Test&?obj)
{
mi=obj.mi;
printf("Test(const?Test&)?obj?is?%d\n",mi);
}
};
int?main()
{
int?i?=?0?;
Test?a1?=i;//Test(int?i):0
while(i<3)
{
Test?a2?=?++i;//Test(int?i):1,2,3
}
if(i<4)
{
Test?a?=?a1;?//Test(const?Test&?obj?is?:0
}
else
{
Test?a(100);
}
return?0;
}
輸出結(jié)果:
Test(int?i)?is?0
Test(int?i)?is?1
Test(int?i)?is?2
Test(int?i)?is?3
Test(const?Test&?obj)?is?0
這里我們可以看出當(dāng)程序流執(zhí)行到相應(yīng)的構(gòu)造對(duì)象的那條執(zhí)行語(yǔ)句時(shí),就會(huì)調(diào)用構(gòu)造函數(shù)(或者拷貝構(gòu)造函數(shù))。goto語(yǔ)句想必大家不陌生,但是都害怕這玩意,下面我們加入goto語(yǔ)句看看會(huì)產(chǎn)生什么現(xiàn)象:
#include?
class?Test{
private:
int?mi;
public:
Test(int?i)
{
mi=i;
printf("Test(int?i)?is?%d\n",mi);
}
Test(const?Test&?obj)
{
mi=obj.mi;
printf("Test(const?Test&?obj?is?%d\n",mi);
}
};
int?main()
{
int?i?=?0;??//Test(int?i)?:0
Test?a1?=?i;
while(?i?<3)
{
Test?a2?=?++i;??//Test(int?i)?:1,2,3
}
goto?end;
if(i?<4)
{
Test?a?=?a1;//Test(const?Test&)?obj?is?:0
}
else
{
Test?a(100);
}
end:
return?0;
}
輸出結(jié)果:
Test(int?i)?is?0
Test(int?i)?is?1
Test(int?i)?is?2
Test(int?i)?is?3
從結(jié)果我們可以看出從if那條語(yǔ)句就被跳過(guò)了,沒(méi)有執(zhí)行到,這里這樣寫(xiě)的目的是為了引出,當(dāng)你使用goto語(yǔ)句,把對(duì)象給屏蔽了,后面你不能使用這個(gè)對(duì)象了,不然程序會(huì)出現(xiàn)大問(wèn)題:
#include?
class?Test{
private:
int?mi;
public:
Test(int?i)
{
mi=i;
printf("Test(int?i)?is?%d\n",mi);
}
Test(const?Test&?obj)
{
mi=obj.mi;
printf("Test(const?Test&?obj?is?%d\n",mi);
}
int?getMi()
{
return?mi;
}
};
int?main()
{
int?i?=?0;??//Test(int?i)?:0
Test?a1?=?i;
while(?i?<3)
{
Test?a2?=?++i;??//Test(int?i)?:1,2,3
}
goto?end;
Test?a(100);
end:
printf("a.mi?is?%d\n",a.getMi());
return?0;
}
輸出結(jié)果:
tt.cpp:?In?function?‘int?main()’:
tt.cpp:32:1:?error:?jump?to?label?‘end’?[-fpermissive]
end:
^
tt.cpp:30:6:?error:???from?here?[-fpermissive]
goto?end;
^
tt.cpp:31:12:?error:???crosses?initialization?of?‘Test?a’
Test?a(100);
^
這里就是上面所說(shuō)了的,對(duì)象被goto語(yǔ)句給屏蔽了,后面就不能使用這個(gè)對(duì)象來(lái)進(jìn)行操作了。
2、對(duì)于堆對(duì)象:
當(dāng)程序執(zhí)行流到達(dá)new語(yǔ)句時(shí)創(chuàng)建對(duì)象
使用new創(chuàng)建對(duì)象將自動(dòng)觸發(fā)構(gòu)造函數(shù)的調(diào)用
代碼演示:
#include?
class?Test
{
private:
int?mi;
public:
Test(int?i)
{
mi?=?i;
printf("Test(int?i):?%d\n",?mi);
}
Test(const?Test&?obj)
{
mi?=?obj.mi;
printf("Test(const?Test&?obj):?%d\n",?mi);
}
int?getMi()
{
return?mi;
}
};
int?main()
{
int?i?=?0;
Test*?a1?=?new?Test(i);?//?Test(int?i):?0
while(?++i?
if(?i?%?2?)
new?Test(i);?//?Test(int?i):?1,?3,?5,?7,?9
if(?i?
new?Test(*a1);
else
new?Test(100);?//?Test(int?i):?100
return?0;
}
輸出結(jié)果:
Test(int?i):?0
Test(int?i):?1
Test(int?i):?3
Test(int?i):?5
Test(int?i):?7
Test(int?i):?9
Test(int?i):?100
3、對(duì)于全局對(duì)象:
對(duì)象的構(gòu)造順序是不確定的
不同的編譯器使用不同的規(guī)則來(lái)確定構(gòu)造順序。
同樣還是來(lái)看代碼示例,這里我創(chuàng)建了幾個(gè)文件:tes1.cpp ? test2.cpp ? test3.cpp ? test4.cpp ? ?test.h;他們的內(nèi)容如下:
test1.cpp:
#include?"test.h"
Test?t4("t4");
int?main()
{
Test?t5("t5");
}
test2.cpp:
#include?"test.h"
Test?t1("t1");
test3.cpp:
#include?"test.h"
Test?t2("t2");
test4.cpp:
#include?"test.h"
Test?t3("t3");
test.h:
#ifndef?_TEST_H_
#define?_TEST_H_
#include?
class?Test
{
public:
Test(const?char*?s)
{
printf("%s\n",?s);
}
};
#endif
最后輸出結(jié)果:
root@txp-virtual-machine:/home/txp#?g++?test1.cpp?test2.cpp?test3.cpp?test4.cpp?-o?put
root@txp-virtual-machine:/home/txp#?./put
t4
t1
t2
t3
t5
4、小結(jié):
局部對(duì)象的構(gòu)造順序依賴程序的執(zhí)行流
堆對(duì)象的構(gòu)造順序依賴于new的使用順序
全局對(duì)象的構(gòu)造順序是不確定的
二、析構(gòu)函數(shù):
1、c++的類(lèi)中可以定義一個(gè)特殊的清理函數(shù),叫做析構(gòu)函數(shù),這個(gè)函數(shù)的功能與構(gòu)造函數(shù)相反,顧名思義就是銷(xiāo)毀的意思了。
2、定義:~ClassName()
析構(gòu)函數(shù)沒(méi)有參數(shù)也沒(méi)有返回值類(lèi)型聲明
析構(gòu)函數(shù)在對(duì)象銷(xiāo)毀時(shí)自動(dòng)被調(diào)用
代碼示例:
#include?
class?Test
{
int?mi;
public:
Test(int?i)
{
mi?=?i;
printf("Test():?%d\n",?mi);
}
~Test()
{
printf("~Test():?%d\n",?mi);
}
};
int?main()
{
Test?t(1);
Test*?pt?=?new?Test(2);
delete?pt;
return?0;
}
輸出結(jié)果:
Test():?1
Test():?2
~Test():?2
~Test():?1
3、析構(gòu)函數(shù)的定義準(zhǔn)則:
當(dāng)類(lèi)中自定義了構(gòu)造函數(shù),并且析構(gòu)函數(shù)中使用了系統(tǒng)資源(比如說(shuō),內(nèi)存的申請(qǐng),文件打開(kāi)),那么就需要自定義析構(gòu)函數(shù)了。
4、小結(jié):
析構(gòu)函數(shù)是對(duì)象銷(xiāo)毀時(shí)進(jìn)行處理的特殊函數(shù)
析構(gòu)函數(shù)在對(duì)象銷(xiāo)毀時(shí)自動(dòng)被調(diào)用
析構(gòu)函數(shù)是對(duì)象釋放系統(tǒng)資源的保障
總結(jié)
以上是生活随笔為你收集整理的Java对象析构_c++之对象构造顺序和销毁(析构函数)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android限制安装包来源,Andro
- 下一篇: 我的LAMP源码编译安装linux+Ap