In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge about the construction order of C++ objects. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
I. the construction order of local objects
For local object
Construct when the program executes the flow to the definition statement of the object
Let's look at an example of the construction of a local object:
# include 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;} 输出结果如下: 如果对象没有被初始化会发生什么,下面看一个示例: #include 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 语句,会跳过初始化 二、堆对象的构造顺序 对于堆对象 当程序执行流到达 new 语句时创建对象 使用 new 创建对象将自动触发构造函数的调用 下面看一个堆空间的构造顺序示例: #include 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;} 输出结果如下: 三、全局对象的构造顺序 对于全局对象 对象的构造顺序是不确定的 不同的编译器使用不同的规则确定构造顺序 下面看一个全局对象的构造顺序示例: test.h: #ifndef _TEST_H_ #define _TEST_H_ #include 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 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 Command prompt
The second step is to enter the folder where you need to compile the utility cd/d. (note that you need to enter / d to change the drive letter)
The file I want to compile is in the C:\ Users\ HuZeQiu\ Desktop\ demo folder.
Enter cd/d C:\ Users\ HuZeQiu\ Desktop\ demo and press enter as follows to go to the destination folder
The third step is to enter the cltest.cpp t2.cpp t1.cpp t3.cpp-otest.exe compiler. (the cl command is used to compile the program) press enter to start the compilation and generate the test.exe executable file, as follows:
Step 4: run test.exe, type test.exe directly, and you can see the running result.
The compiled folder is as follows:
These are all the contents of the article "what is the order in which C++ objects are constructed?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.