網(wǎng)站首頁 編程語言 正文
一、局部對象的構造順序
對于局部對象
當程序執(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ù)的調(diào)用
下面看一個堆空間的構造順序示例:
#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 的使用順序
- 全局對象的構造順序是不確定的
原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/123643118
相關推薦
- 2023-05-20 Android內(nèi)存泄漏導致原因深入探究_Android
- 2022-01-17 url傳參 中文出現(xiàn)亂碼問題 解決方案
- 2022-07-10 css選擇器優(yōu)先級問題
- 2022-05-12 小程序滾動穿透解決方案
- 2022-12-15 C++進程鏈接工具之通信器詳解_C 語言
- 2023-01-18 Golang?sync.Map原理深入分析講解_Golang
- 2022-05-21 基于C++實現(xiàn)酒店管理系統(tǒng)_C 語言
- 2022-11-30 Git基礎學習之分支基本操作詳解_相關技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支