经常在程序中碰到#ifdef WIN32这样的代码,实际上这提供了一种抽象机制,使代码可以在不同平台间移植. 下面给出一个简单的例子:
- #include<iostream>
- using namespace std;
-
- #ifdef WIN32
- #define HAHA 1
- #else
- #define HAHA 0
- #endif
-
- int main()
- {
- int i = HAHA;
- cout << i << endl;
-
- return 0;
- }
程序结果为:1
为什么是1呢? 程序中没有看到有WIN32的定义啊,不慌,点击“工程”“设置”“C/C++”, 看到了下面图片,哦,原来如此!