C++中如何在main()函数之前执行操作?

本文介绍了在C++中如何实现在main函数之前执行特定操作的方法。通过创建全局对象或静态对象,在main函数启动前自动调用其构造函数。此外,还介绍了_onexit/atexit函数的使用,该函数允许开发者指定在程序正常终止时调用的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++中如何在main()函数之前执行操作?

           第一反应main()函数是所有函数执行的开始。但是问题是main()函数执行之前如何执行呢?

           联想到MFC里面的 C**App类的theApp对象,其执行顺序就在main函数之前。道理相通,顺理推下,能够想到:如果在main函数之前声明一个类的全局的对象。那么其执行顺序,根据全局对象的生存期和作用域,肯定先于main函数。

示例如下:

[cpp]  view plain copy
  1. <span style="font-size:14px;">class simpleClass  
  2. {  
  3. public:  
  4.        simpleClass( )  
  5.        {  
  6.               cout << "simpleClass constructor.." << endl;  //step2  
  7.        }  
  8. };  
  9.   
  10.    
  11. simpleClass g_objectSimple;         //step1全局对象  
  12.   
  13. int _tmain(int argc, _TCHAR* argv[])  //step3  
  14. {  
  15.        return 0;  
  16. }  
  17.   
  18.   
  19. 可单步调试查看执行顺序为step1、step2、step3。</span>  

 

考虑到全局对象,同理会进一步思考静态对象的作用域。将上述示例进一步扩展如下:

[cpp]  view plain copy
  1. <span style="font-size:14px;">class simpleClass  
  2. {  
  3. public:  
  4.        simpleClass( )  
  5.        {  
  6.               cout << "simpleClass constructor.." << endl;       //step2   
  7.        }  
  8. };  
  9.   
  10. class simpleClassTwo  
  11. {  
  12. public:  
  13.        static simpleClass m_sSimpleClass;  
  14. };  
  15.   
  16. simpleClass simpleClassTwo::m_sSimpleClass = simpleClass(); //step1 静态对象  
  17.   
  18. int _tmain(int argc, _TCHAR* argv[])                        //step3  
  19.   
  20. {  
  21.         return 0;  
  22. }  
  23.   
  24.   
  25. 可单步调试查看执行顺序为step1、step2、step3。</span>  

        至此,我们可以总结出:定义在main( )函数之前的全局对象、静态对象的构造函数在main( )函数之前执行

      再进一步思考,既然可以在main( )函数之前执行全局、静态对象的构造函数。那么有没有函数在main( )函数之后执行呢?

有的,onexit函数。原型如下:

_onexit_t _onexit(

   _onexit_t function

);

_onexit_t_m _onexit_m(

   _onexit_t_m function

);

解释:The _onexit function is passed the address of a function (function) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.

 

核心点:

1)              执行期——程序执行终止的时候;

2)              传递参数——函数的地址,即函数指针;

3)              执行顺序——后进先出。

 

_onexit is a Microsoft extension. For ANSI portability, use atexit. The _onexit_m version of the function is for mixed mode use.

onexit是微软的扩展版本,标准C++里面应用的是atexit。

【MSDN】示例:

[cpp]  view plain copy
  1. <span style="font-size:14px;">#include <stdlib.h>  
  2. #include <stdio.h>  
  3. /* Prototypes */  
  4. int fn1(void), fn2(void), fn3(void), fn4 (void);  
  5.   
  6. int main( void )  
  7. {  
  8.       _onexit( fn1 );  
  9.       _onexit( fn2 );  
  10.       _onexit( fn3 );  
  11.       _onexit( fn4 );  
  12.       printf( "This is executed first.\n" );  
  13. }  
  14.   
  15.    
  16. int fn1()  
  17. {  
  18.       printf( "next.\n" );  
  19.       return 0;  
  20. }  
  21.   
  22.    
  23. int fn2()  
  24. {  
  25.       printf( "executed " );  
  26.       return 0;  
  27. }  
  28.   
  29.    
  30. int fn3()  
  31. {  
  32.       printf( "is " );  
  33.       return 0;  
  34. }  
  35.   
  36.    
  37. int fn4()  
  38. {  
  39.       printf( "This " );  
  40.       return 0;  
  41. }  
  42.   
  43. </span>  

执行结果如下:

       显然,读程序可以看出main( )函数执行完毕后又执行了onexit( )函数。

      析构函数主要是指当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数。确切的说其和main()函数关系不大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值