全部变量、静态全局变量、类静态变量、atexit回调函数的调用顺序
来源:互联网 发布:电信宽带无法连接网络 编辑:程序博客网 时间:2024/06/05 04:02
巩固下c++基础,代码如下:
#include <iostream>#include <vector>#include <map>#include <string>std::vector<void*> structure;std::map<void*, std::string> info;class test1{public:test1(){structure.push_back(this);}~test1(){std::cout<<this<<"---~test1\n";}};class test2{public:test2(){structure.push_back(this);}~test2(){std::cout<<this<<"---~test2\n";}static test1 test1_;};class test3{public:test3(){structure.push_back(this);}~test3(){std::cout<<this<<"---~test3\n";}};class test4{public:test4(){structure.push_back(this);}~test4(){std::cout<<this<<"---~test4\n";}static test3 test3_;};void OnExit(){std::cout<<"--------------------------------------\n";std::cout<<"Call OnExit\n";}static test3 static_test3_;static test1 static_test1_;test2 test2_;test1 test1_;test3 test4::test3_;test1 test2::test1_;int main(int argc, _TCHAR* argv[]){std::cout<<"--------------------------------------\n";std::cout<<"Regist OnExit\n";atexit(OnExit);info[&test2_] = "全局变量test2_";info[&test1_] = "全局变量test1_";info[&static_test1_] = "静态全局变量test1_";info[&static_test3_] = "静态全局变量test3_";info[&test2::test1_] = "静态类变量test2::test1_";info[&test4::test3_] = "静态类变量test4::test3_";std::cout<<"------------------构造----------------\n";for(auto iter = structure.begin(), end = structure.end(); iter != end; ++iter){std::cout<<*iter<<"---"<<info[*iter]<<"\n";}return 0;}
执行结果:
总结:
1.全局变量,静态全局变量,类静态变量的构造顺序依赖于其定义顺序。
2.全局变量,静态全局变量,类静态变量的析构顺序和构造顺序相反。
3.atexit回调函数调用先与全局变量,静态全局变量,类静态变量析构函数调用。
- 全部变量、静态全局变量、类静态变量、atexit回调函数的调用顺序
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数中调用类中的非静态成员变量或非静态成员函数
- 回调函数(类静态函数)中调用类中的非静态成员变量或非静态成员函数
- Qt 静态回调函数,访问类成员变量,解决办法
- 回调函数中如何调用类中的非静态成员变量或非静态成员函数
- [转]回调函数中调用类中的非静态成员变量或非静态成员函数
- 全局变量、局部变量、静态全局变量、静态局部变量的区别
- 全局变量 局部变量 静态全局变量 静态局部变量的区别
- 全局变量、局部变量、静态全局变量、静态局部变量的区别
- window.onresize 多次触发及其解决办法 - debounce
- “应用程序配置不正确, 程序无法启动”解决方法
- selenium webdriver 学习总结-JUnit4 入门_补充Annotation(三)
- Win32编程实现剪贴板监控查看
- VS2010搭建SVN,实现代码的版本控制----【SVN服务器搭建】
- 全部变量、静态全局变量、类静态变量、atexit回调函数的调用顺序
- WPF中把自定义类型对象拖出去,拖进来
- Centralized Logging Architecture
- Hbase counter
- selenium webdriver 学习总结-Selenium 控制测试流_补充显示等待(五)
- ListView
- 4种HTML结构化解析方法
- Binary Tree Level Order Traversal 分层遍历二叉树@LeetCode
- NGUITools.AddChild