C++综合系列之构造函数执行于main函数之前

时间:2021-11-26 19:25:38
#include<iostream>
using namespace std;

class Pre_Main
{
public:
Pre_Main()
{
cout << "I'm before main!" << endl;
}
~Pre_Main()
{
cout << "I'm after main!" << endl;
}
};

//在C++中,利用全局变量和构造函数的特性,通过全局变量的构造函数执行;
Pre_Main pre_Main;

void main()
{
cout << "I'm in main!" << endl;
}