網(wǎng)站首頁 編程語言 正文
一、局部對象的構(gòu)造順序
對于局部對象
當程序執(zhí)行流到達對象的定義語句時進行構(gòu)造
下面看一個局部對象的構(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 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;
}
輸出結(jié)果如下:
如果對象沒有被初始化會發(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 語句,會跳過初始化
二、堆對象的構(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;
}
輸出結(jié)果如下:
三、全局對象的構(gòu)造順序
對于全局對象
- 對象的構(gòu)造順序是不確定的
- 不同的編譯器使用不同的規(guī)則確定構(gòu)造順序
下面看一個全局對象的構(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
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 編譯器中,輸出結(jié)果如下:
下面看一下使用 VS2012 編譯這些代碼:
(不知道 VS2012怎么使用命令行窗口編譯程序的可以看《命令行》不需要可以跳過)
這足以說明全局變量的構(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
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,按下回車鍵,如下,就轉(zhuǎn)到了目的文件夾
第三步,輸入 cltest.cpp t2.cpp t1.cpp t3.cpp -otest.exe 編譯程序。(cl 命令是用來編譯程序)按下回車鍵后開始編譯,生成 test.exe 可執(zhí)行文件,如下:
第四步,運行 test.exe,直接輸入 test.exe 即可,就可以看到運行結(jié)果
編譯后的文件夾如下:
四、小結(jié)
- 局部對象的構(gòu)造順序依賴于程序的執(zhí)行流
- 堆對象的構(gòu)造順序依賴于 new 的使用順序
- 全局對象的構(gòu)造順序是不確定的
原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/123643118
相關(guān)推薦
- 2022-08-13 Redis 性能影響 - 異步機制和響應(yīng)延遲
- 2022-03-08 android?studio項目:綁定服務(wù)和線程實現(xiàn)計時器_Android
- 2022-07-09 document.write() 的作用*
- 2022-06-18 C#實現(xiàn)多線程編程的簡單案例_C#教程
- 2022-02-13 搞明白this指向,走遍天下都不怕(一)
- 2022-07-29 Redis入門基礎(chǔ)常用操作命令整理_Redis
- 2022-08-31 ASP.NET?Core中的對象池介紹_實用技巧
- 2023-11-14 樹莓派上如何安裝anaconda/miniconda環(huán)境配置
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支