1)函数属性功能
__attribute__ ((constructor))会使函数在main()函数之前被执行
__attribute__ ((destructor))会使函数在main()退出后执行
2)功能范围
函数属性__attribute__((constructor))和__attribute__((destructor))在可执行文件或者库文件里都可以生效
3)与全局变量比较
全局变量对象的构造函数和析构函数分别在__attribute__((constructor))和__attribute__((destructor))标志的函数之前调用。
1、可执行文件的函数调用
例子如下:
#include <iostream>
//函数属性声明
void beforemain() __attribute__((constructor));
void aftermain() __attribute__((destructor));
void breforemain()
{
std::cout << "before main function" << std::endl;
}
void aftermain()
{
std::cout << "after main function" << std::endl;
}
class AAA{
public:
AAA(){std::cout << "global veriable constructor" << std::endl;}
~AAA(){std::cout << "global veriable destructor" << std::endl;}
};