在main函数之外执行函数的情况

COPY FROM:http://blog.csdn.net/cjc211322/article/details/38702649

1、onexit函数

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include "iostream"  
  3. #include <stdlib.h>  
  4. using namespace std;  
  5.   
  6. int func()                              //必须为int返回值  
  7. {  
  8.     cout<<"This is after main function"<<endl;  
  9.     system("pause");  
  10.     return 0;  
  11. }  
  12.   
  13. int main(int argc,char*argv[])  
  14. {  
  15.     onexit(func);  
  16.     cout<<"This is main function"<<endl;  
  17.     system("pause");  
  18.     return 0;  
  19. }  


2、全局对象的构建

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include "iostream"  
  3. #include <stdlib.h>  
  4. using namespace std;  
  5.   
  6. class A  
  7. {  
  8. public:  
  9.     A()  
  10.     {  
  11.         cout<<"This is A's constructor"<<endl;  
  12.     }  
  13. };  
  14.   
  15. A a;  
  16.   
  17. int main(int argc,char*argv[])  
  18. {  
  19.     cout<<"This is the main function"<<endl;  
  20.     system("pause");  
  21.     return 0;  
  22. }  


3、析构函数的调用

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include "iostream"  
  3. #include <stdlib.h>  
  4. using namespace std;  
  5.   
  6. class A  
  7. {  
  8. public:  
  9.     A()  
  10.     {  
  11.         cout<<"This is A's constructor"<<endl;  
  12.     }  
  13.     ~A()  
  14.     {  
  15.         cout<<"This is A's deconstructor"<<endl;  
  16.         system("pause");  
  17.     }  
  18. };  
  19.   
  20. A(a);  
  21. int main(int argc,char*argv[])  
  22. {  
  23.     cout<<"This is the main function"<<endl;  
  24.     system("pause");  
  25.     return 0;  
  26. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值