一、對象的構造順序:
1、對于局部對象:
當程序執行流到達對象的定義語句時進行構造。下面還是用代碼來解析這句話:
#include <stdio.h>
class Test
{
private:
int mi;
public:
Test(int i)
{
mi=i;
printf("Test(int i) is %d",mi);
}
Test(const Test& obj)
{
mi=obj.mi;
printf("Test(const Test&) obj is %d",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;
}
輸出結果:
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
這里我們可以看出當程序流執行到相應的構造對象的那條執行語句時,就會調用構造函數(或者拷貝構造函數)。goto語句想必大家不陌生,但是都害怕這玩意,下面我們加入goto語句看看會產生什么現象:
#include <stdio.h>
class Test{
private:
int mi;
public:
Test(int i)
{
mi=i;
printf("Test(int i) is %d",mi);
}
Test(const Test& obj)
{
mi=obj.mi;
printf("Test(const Test& obj is %d",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;
}
輸出結果:
Test(int i) is 0
Test(int i) is 1
Test(int i) is 2
Test(int i) is 3
從結果我們可以看出從if那條語句就被跳過了,沒有執行到,這里這樣寫的目的是為了引出,當你使用goto語句,把對象給屏蔽了,后面你不能使用這個對象了,不然程序會出現大問題:
#include <stdio.h>
class Test{
private:
int mi;
public:
Test(int i)
{
mi=i;
printf("Test(int i) is %d",mi);
}
Test(const Test& obj)
{
mi=obj.mi;
printf("Test(const Test& obj is %d",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",a.getMi());
return 0;
}
輸出結果:
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);
^
這里就是上面所說了的,對象被goto語句給屏蔽了,后面就不能使用這個對象來進行操作了。
2、對于堆對象:
當程序執行流到達new語句時創建對象
使用new創建對象將自動觸發構造函數的調用
代碼演示:
#include <stdio.h>
class Test
{
private:
int mi;
public:
Test(int i)
{
mi = i;
printf("Test(int i): %d", mi);
}
Test(const Test& obj)
{
mi = obj.mi;
printf("Test(const Test& obj): %d", mi);
}
int getMi()
{
return mi;
}
};
int main()
{
int i = 0;
Test* a1 = new Test(i); // Test(int i): 0
while( ++i < 10 )
if( i % 2 )
new Test(i); // Test(int i): 1, 3, 5, 7, 9
if( i < 4 )
new Test(*a1);
else
new Test(100); // Test(int i): 100
return 0;
}
輸出結果:
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、對于全局對象:
對象的構造順序是不確定的
不同的編譯器使用不同的規則來確定構造順序。
同樣還是來看代碼示例,這里我創建了幾個文件:tes1.cpp test2.cpp test3.cpp test4.cpp test.h;他們的內容如下:
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 <stdio.h>
class Test
{
public:
Test(const char* s)
{
printf("%s", s);
}
};
#endif
最后輸出結果:
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、小結:
局部對象的構造順序依賴程序的執行流
堆對象的構造順序依賴于new的使用順序
全局對象的構造順序是不確定的
二、析構函數:
1、c++的類中可以定義一個特殊的清理函數,叫做析構函數,這個函數的功能與構造函數相反,顧名思義就是銷毀的意思了。
2、定義:~ClassName()
析構函數沒有參數也沒有返回值類型聲明
析構函數在對象銷毀時自動被調用
代碼示例:
#include <stdio.h>
class Test
{
int mi;
public:
Test(int i)
{
mi = i;
printf("Test(): %d", mi);
}
~Test()
{
printf("~Test(): %d", mi);
}
};
int main()
{
Test t(1);
Test* pt = new Test(2);
delete pt;
return 0;
}
輸出結果:
Test(): 1
Test(): 2
~Test(): 2
~Test(): 1
3、析構函數的定義準則:
當類中自定義了構造函數,并且析構函數中使用了系統資源(比如說,內存的申請,文件打開),那么就需要自定義析構函數了。
4、小結:
析構函數是對象銷毀時進行處理的特殊函數
析構函數在對象銷毀時自動被調用
析構函數是對象釋放系統資源的保障
-
函數
+關注
關注
3文章
4346瀏覽量
62972 -
C++
+關注
關注
22文章
2114瀏覽量
73857
發布評論請先 登錄
相關推薦
Spire.XLS for C++組件說明
![Spire.XLS for <b class='flag-5'>C++</b>組件說明](https://file1.elecfans.com/web3/M00/05/E7/wKgZO2eFwUuAbuoQAAAbn_khf8A091.png)
同樣是函數,在C和C++中有什么區別
SpringBean初始化順序
![SpringBean初始化<b class='flag-5'>順序</b>](https://file.elecfans.com/web2/M00/8A/6C/pYYBAGO9DiuANww7AABrMCuQJho521.png)
ostream在c++中的用法
OpenVINO2024 C++推理使用技巧
C++中實現類似instanceof的方法
![<b class='flag-5'>C++</b>中實現類似instanceof的方法](https://file1.elecfans.com/web2/M00/FE/0C/wKgaomaYe1CAQ31QAAAnf0IkoSU605.png)
鴻蒙OS開發實例:【Native C++】
![鴻蒙OS開發實例:【Native <b class='flag-5'>C++</b>】](https://file1.elecfans.com/web2/M00/C8/31/wKgZomYZMTCAaDv3AAY5x13C324319.jpg)
使用 MISRA C++:2023? 避免基于范圍的 for 循環中的錯誤
![使用 MISRA <b class='flag-5'>C++</b>:2023? 避免基于范圍的 for 循環中的錯誤](https://file1.elecfans.com/web2/M00/A9/66/wKgZomUl7m-AHJX6AABuJjgxs14678.png)
為什么很少用C++開發單片機
![為什么很少用<b class='flag-5'>C++</b>開發單片機](https://file1.elecfans.com/web2/M00/C6/8F/wKgaomYBGYeAEfT2AAAsOmCJ4d8781.png)
評論