文章内容是我自己遇到的问题总结出来的,不一定正确,望指正。
我定义一个类,并且在其后添加__s_onexit_pause关键字,那么程序会自动运行类的构造函数。如下代码:
class OnApp {
public:
OnApp() {
// Initialize Winsock 2.2
WSADATA wsaData;
int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
cout << "******************" << endl;
cout << "出书画" << endl;
if (err) {
std::cout << "WSAStartup() failed with error: %d" << err;
}
}
//void test() { cout << "444444" << endl; }
~OnApp() {
system("pause");
//cout << ""
WSACleanup();
}
} __s_onexit_pause;//这里添加了__s_onexit_pause关键字
int main(int argc, char* argv[]) {
int a = 4;
return 0;
}
运行结果如下,可以看出程序是没有经过创建对象就调用了构造函数。
******************
出书画
如果没有添加这个关键字的话则不会。