在main函数之外执行函数的情况
2014-11-24 18:51
387 查看
COPY FROM:http://blog.csdn.net/cjc211322/article/details/38702649
[cpp] view
plaincopy
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
using namespace std;
int func() //必须为int返回值
{
cout<<"This is after main function"<<endl;
system("pause");
return 0;
}
int main(int argc,char*argv[])
{
onexit(func);
cout<<"This is main function"<<endl;
system("pause");
return 0;
}
[cpp] view
plaincopy
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
using namespace std;
class A
{
public:
A()
{
cout<<"This is A's constructor"<<endl;
}
};
A a;
int main(int argc,char*argv[])
{
cout<<"This is the main function"<<endl;
system("pause");
return 0;
}
[cpp] view
plaincopy
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
using namespace std;
class A
{
public:
A()
{
cout<<"This is A's constructor"<<endl;
}
~A()
{
cout<<"This is A's deconstructor"<<endl;
system("pause");
}
};
A(a);
int main(int argc,char*argv[])
{
cout<<"This is the main function"<<endl;
system("pause");
return 0;
}
1、onexit函数
[cpp] viewplaincopy
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
using namespace std;
int func() //必须为int返回值
{
cout<<"This is after main function"<<endl;
system("pause");
return 0;
}
int main(int argc,char*argv[])
{
onexit(func);
cout<<"This is main function"<<endl;
system("pause");
return 0;
}
2、全局对象的构建
[cpp] viewplaincopy
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
using namespace std;
class A
{
public:
A()
{
cout<<"This is A's constructor"<<endl;
}
};
A a;
int main(int argc,char*argv[])
{
cout<<"This is the main function"<<endl;
system("pause");
return 0;
}
3、析构函数的调用
[cpp] viewplaincopy
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
using namespace std;
class A
{
public:
A()
{
cout<<"This is A's constructor"<<endl;
}
~A()
{
cout<<"This is A's deconstructor"<<endl;
system("pause");
}
};
A(a);
int main(int argc,char*argv[])
{
cout<<"This is the main function"<<endl;
system("pause");
return 0;
}
相关文章推荐
- 在main函数之外执行函数的情
- XAML加载情况判断并延时执行函数
- main函数之前--真正的函数执行入口或开始
- MATLAB中nargin函数的用法nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。通常可以用它来设定一些默认值。如下例所示: 函数文件 examp.m
- [Hb-XVII] 计算机的抽象层次-简 使用寄存器 使用内存空间 程序执行过程 使用main函数规定 不定参数函数机制 C
- 使用system,popen等执行命令函数失败的情况
- 任何C++程序首先执行的函数是main函数?
- matlab中“执行字符串”函数eval在不同情况下的用法(二)
- main函数之前--真正的函数执行入口或开始
- C语言中如何在main函数开始前执行函数
- 在main函数前后执行的函数之 C语言
- C语言中如何在main函数开始前执行函数
- 一个超简单,输出main函数返回值的函数,且是测试没有return语句的情况下哦~
- mian函数之前执行函数,和main函数之后执行函数
- atexit注册的函数是在main函数之后执行?
- main函数之前--真正的函数执行入口或开始
- XAML加载情况判断并延时执行函数
- main函数之前--真正的函数执行入口或开始
- Android中切换屏幕方向时Activity生命周期函数执行情况分析
- main函数开始前执行函数