标签:des c style class blog code
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
网上有说可以用
__attribute__ ((constructor)) 来让函数在main函数之前执行,
__attribute__ ((destructor)) 来让函数在main函数之后执行。
比如说像下面这样声明函数
void before(void) __attribute__ ((constructor)); void after(void) __attribute__ ((destructor)
在标准C/C++中
可以用global variable 或static variable来让代码在main函数之前执行
可以用atexit来让函数在main函数之后执行
#include <iostream>
using namespace std;
int before_main(){
cout << "before main" << endl;
return 1;
}
static int a = before_main();
void after_main(){
cout << "after main" << endl;
}
int main(int argc, char *argv[])
{
cout << "main" << endl;
atexit(after_main);
system("pause");
}
怎样使代码在main函数前执行,怎样使代码在main函数之后执行,布布扣,bubuko.com
怎样使代码在main函数前执行,怎样使代码在main函数之后执行
标签:des c style class blog code
原文地址:http://blog.csdn.net/zhengsenlie/article/details/27108921