您的位置:首页 > 大数据 > 人工智能

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

2014-11-24 18:51 387 查看
COPY FROM:http://blog.csdn.net/cjc211322/article/details/38702649


1、onexit函数

[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;  

}  




2、全局对象的构建

[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;  

}  




3、析构函数的调用

[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;  

}  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  main函数