码迷,mamicode.com
首页 > 其他好文 > 详细

在main函数之外执行函数的情况

时间:2014-08-20 12:38:22      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:main函数外调用函数的情况

1、onexit函数

#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;
}
bubuko.com,布布扣

2、全局对象的构建

#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;
}
bubuko.com,布布扣

3、析构函数的调用

#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");
	}
};

int main(int argc,char*argv[])
{
	A(a);
	system("pause");
	return 0;
}
bubuko.com,布布扣

在main函数之外执行函数的情况,布布扣,bubuko.com

在main函数之外执行函数的情况

标签:main函数外调用函数的情况

原文地址:http://blog.csdn.net/cjc211322/article/details/38702649

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!