运行结果:
然后再更换为以下代码:
#include <afxwin.h> class MyApp : public CWinApp { public: BOOL InitInstance() //②程序入点 { AfxMessageBox(_T("程序依然可以运行!")); return true; //返回 } }; MyApp theApp; //①建立应用程序。
程序运行结果为:
我们知道,C++控制台程序的入口点函数为main()函数,而Windows应用程序的入口点函数为WinMain()。然而,上述程序并没有main()或WinMain()函数,也能运行。实际上,在main()或WinMain()函数执行之前,全局对象会先运行。在上述程序中我们定义了全局对象theApp,程序会首先执行theApp。只要我们构造了CWinApp 对象,就可以执行WinMain()函数。
例如,我们再建立一个Win32控制台程序,代码如下:
#include<iostream> using namespace std; class test { public: test() { cout<<"请改变你对main()函数的看法!"<<endl; } }; test test1; int main() { system("pause"); return 0; }四川中医肝病医院