在 C++ 中的類可以定義多個對象,那么對象構(gòu)造的順序是怎樣的呢?對于局部對象:當程序執(zhí)行流到達對象的定義語句時進行構(gòu)造。我們以代碼為例進行分析
創(chuàng)新互聯(lián)是一家專業(yè)提供贊皇企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、H5建站、小程序制作等業(yè)務。10年已為贊皇眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
#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; // 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): 0 } else { Test a(100); } return 0; }
我們按照程序的執(zhí)行流可以看到先是執(zhí)行對象 a1 的創(chuàng)建,接著是對象 a2 的創(chuàng)建 3 次,最后是對象 a 的拷貝構(gòu)造。我們看看結(jié)果是否如我們所分析的那樣
我們看到局部對象的構(gòu)造順序確實如我們所想的那樣。如果我們使用 goto 語句呢,我們看個代碼
#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; // Test(int i): 0 while( i < 3 ) { Test a2 = ++i; // Test(int i): 1, 2, 3 } goto End; Test a(100); End: printf("a.mi = %d\n", a.getMI()); return 0; }
我們來編譯看看
編譯直接出錯,因為我們使用了 goto 語句,導致程序的執(zhí)行流出錯了。
接下來我們來看看堆對象的構(gòu)造順序,當程序執(zhí)行流到達 new 語句時創(chuàng)建對象,使用 new 創(chuàng)建對象將自動觸發(fā)構(gòu)造函數(shù)的調(diào)用。
下來還是以代碼為例來分析堆對象的構(gòu)造順序
#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; }
我們看看是否如我們所注釋的那樣執(zhí)行的
確實,堆對象的構(gòu)造順序是跟 new 關(guān)鍵字有關(guān)系的。下來我們來看看全局對象,對象的構(gòu)造順序是不確定的,不同的編譯器使用不同的規(guī)則來確定構(gòu)造順序。還是以代碼為例來進行驗證
test.h 源碼
#ifndef _TEST_H_ #define _TEST_H_ #include <stdio.h> class Test { public: Test(const char* s) { printf("%s\n", s); } }; #endif
t1.cpp 源碼
#include "test.h" Test t1("t1");
t2.cpp 源碼
#include "test.h" Test t2("t2");
t3.cpp 源碼
#include "test.h" Test t3("t3");
test.cpp 源碼
#include "test.h" Test t4("t4"); int main() { Test t5("t5"); return 0; }
我們來編譯看看結(jié)果
這個結(jié)果貌似跟我們指定編譯的順序有關(guān)系,我們再來看看BCC編譯器呢
再來試試 VS2010
以前博主在書上和視頻中看到過全局對象的構(gòu)造順序是不確定的,可能現(xiàn)在的編譯器做了優(yōu)化吧。反正我們記住就可以了,盡量避免使用全局對象。通過對對象的構(gòu)造順序的學習,總稽核如下:局部對象的構(gòu)造順序依賴于程序的執(zhí)行流;堆對象的構(gòu)造順序依賴于 new 的使用順序;全局對象的構(gòu)造順序是不確定的
歡迎大家一起來學習 C++ 語言,可以加我QQ:243343083。
名稱欄目:對象的構(gòu)造順序(十六)
URL地址:http://www.rwnh.cn/article2/gpodic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)頁設(shè)計公司、、外貿(mào)建站、網(wǎng)站營銷、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)