C++ 詳細講解對象的構造順序
一、局部對象的構造順序
對于局部對象
當程序執(zhí)行流到達對象的定義語句時進行構造
下面看一個局部對象的構造示例:
#include <stdio.h> 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 main() { int i = 0; Test a1 = i; while( i < 3 ) { Test a2 = ++i; } if( i < 4 ) { Test a = a1; } else { Test a(100); } return 0; }
輸出結果如下:
如果對象沒有被初始化會發(fā)生什么,下面看一個示例:
#include <stdio.h> 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 = i; while( i < 3 ) { Test a2 = ++i; } goto End; Test a(100); End: printf("a.mi = %d\n", g.getMi()); return 0; }
在 g++ 編譯器下,就會報錯,讓不要使用 goto 語句,會跳過初始化
二、堆對象的構造順序
對于堆對象
- 當程序執(zhí)行流到達 new 語句時創(chuàng)建對象
- 使用 new 創(chuàng)建對象將自動觸發(fā)構造函數(shù)的調用
下面看一個堆空間的構造順序示例:
#include <stdio.h> 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 < 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; }
輸出結果如下:
三、全局對象的構造順序
對于全局對象
- 對象的構造順序是不確定的
- 不同的編譯器使用不同的規(guī)則確定構造順序
下面看一個全局對象的構造順序示例:
test.h:
#ifndef _TEST_H_ #define _TEST_H_ #include <stdio.h> class Test { public: Test(const char* s) { printf("%s\n", s); } }; #endif
test.cpp:
#include "test.h" Test t4("t4"); int main() { Test t5("t5"); }
t1.cpp:
#include "test.h" Test t1("t1");
t2.cpp:
#include "test.h" Test t2("t2");
t3.cpp:
#include "test.h" Test t3("t3");
在 gcc 編譯器中,輸出結果如下:
下面看一下使用 VS2012 編譯這些代碼:
(不知道 VS2012怎么使用命令行窗口編譯程序的可以看《命令行》不需要可以跳過)
這足以說明全局變量的構造順序是不確定的。
命令行
以下面的代碼為例
test.h:
#ifndef _TEST_H_ #define _TEST_H_ #include <stdio.h> class Test { public: Test(const char* s) { printf("%s\n", s); } }; #endif
test.cpp:
#include "test.h" Test t4("t4"); int main() { Test t5("t5"); }
t1.cpp:
#include "test.h" Test t1("t1");
t2.cpp:
#include "test.h" Test t2("t2");
t3.cpp:
#include "test.h" Test t3("t3");
第一步,打開 VS2012,選擇 工具 -> Visual Studio 命令提示
第二步,實用 cd/d 進入需要編譯的文件夾。(注意換盤符需要輸入/d)
我想要編譯的文件在C:\Users\HuZeQiu\Desktop\demo 文件夾里。
輸入cd/d C:\Users\HuZeQiu\Desktop\demo,按下回車鍵,如下,就轉到了目的文件夾
第三步,輸入 cltest.cpp t2.cpp t1.cpp t3.cpp -otest.exe 編譯程序。(cl 命令是用來編譯程序)按下回車鍵后開始編譯,生成 test.exe 可執(zhí)行文件,如下:
第四步,運行 test.exe,直接輸入 test.exe 即可,就可以看到運行結果
編譯后的文件夾如下:
四、小結
- 局部對象的構造順序依賴于程序的執(zhí)行流
- 堆對象的構造順序依賴于 new 的使用順序
- 全局對象的構造順序是不確定的
到此這篇關于C++ 詳細講解對象的構造順序的文章就介紹到這了,更多相關C++ 對象構造順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言使用ffmpeg實現(xiàn)單線程異步的視頻播放器
這篇文章主要為大家詳細介紹了C語言如何使用ffmpeg實現(xiàn)單線程異步的視頻播放器功能,文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下2022-12-12在Visual Studio Code中配置C++編譯環(huán)境的問題
關于Visual Studio Code對C++環(huán)境的配置方法應該有好多種,我這里用到了其中的兩種,具體內(nèi)容詳情文中給大家詳細介紹,對Visual Studio Code配置C++編譯環(huán)境相關知識感興趣的朋友一起看看吧2021-07-07Qt物聯(lián)網(wǎng)管理平臺之實現(xiàn)自動清理早期數(shù)據(jù)功能
隨著時間的增加,存儲的歷史記錄也在不斷增加,如果設備數(shù)量很多,存儲間隔很短,不用多久,數(shù)據(jù)庫中的記錄就非常多,至少是百萬級別起步,而且有些用戶還是需要存儲每一次的采集的數(shù)據(jù)。本文將利用Qt實現(xiàn)自動清理早期數(shù)據(jù),需要的可以參考一下2022-07-07