全局对象在程序进入main之前construct, 离开main后destruct
来源:互联网 发布:zsysecdesk是什么软件 编辑:程序博客网 时间:2024/05/21 07:56
Given the program:
#include<iostream.h>// DON'T use <iostream> or using namespace std;
main() {
cout <<"Hello world" <<endl;
}
modify it to produce the output:
Initialize
Hello world
Clean up
ps: Do not change the function main() in any way. and only modify this cpp file.
Answer: 利用全局对象在程序进入main之前construct, 离开main后destruct之特点
#include<iostream.h>// DON'T use <iostream> or using namespace std;
class A {
public:
A() { cout<<"Initialize: " <<endl; }
~A(){ cout<<"Clean up " <<endl;}
};
A test; //建立一个全局对象,调用 A::A()
main() {
cout <<"Hello world" <<endl;
}
// 析构: 调用A::~()
对于C程序通过设置函数属性为constuctor, 可使其在main()之前运行:
#include<stdio.h>
void first() __attribute__((constructor));
void first()
{
printf("this is function %s/n",__FUNCTION__);
return;
}
int main(int argc,char **argv)
{
printf("this is function %s/n",__FUNCTION__);
return 0;
}
运行结果:
this is function first
this is function main
this is function main
- 全局对象在程序进入main之前construct, 离开main后destruct
- 全局对象在程序进入main之前construct, 离开main后destruct
- 全局对象在程序进入main之前construct, 离开main后destruct
- c++ 在main 之前的全局对象
- How to construct/destruct before/after main()
- main 之前与之后
- main 之前与之后
- 全局对象的构造函数会在main函数之前执行?
- 运行库:程序进入main()之前发生了什么
- 在main()之前执行
- 跑在main之前
- C++程序进入main函数之前,退出main函数之后会做些什么?
- [转]main 之前与之后
- main执行之前与之后
- [转]main 之前与之后
- main 函数之前与之后
- 进入main函数之前做了什么?
- 如何让一段程序在main函数之前执行
- C语言printf——动态指定输出格式长度
- linux 内核的一些特性
- UVa 297 Quadtrees(模拟&四分树)
- 快速组合数
- struts2中Action中设置和取得request/session/application的方法
- 全局对象在程序进入main之前construct, 离开main后destruct
- UIWebView背景透明且无拖拽后的上下阴影
- C++中extern “C”含义深层探索
- 这是第一篇博客,作为测试
- hdu 2391 Filthy Rich
- MVC模式基本理解
- Matlab高性能编程——代码优化和并行计算
- linux内核轻量级虚拟化之Namespace
- 贴图透明特效的实现