靈活使用#ifdef指示符,我們可以區(qū)隔一些與特定頭文件、程序庫和其他文件版本有關(guān)的代碼。 代碼舉例:新建define.cpp文件
#include "iostream.h" int main() { #ifdef DEBUG cout<< "Beginning execution of main()"; #endif return 0; } 運(yùn)行結(jié)果為:Press any key to continue 改寫代碼如下: #include "iostream.h" #define DEBUG int main() { #ifdef DEBUG cout<< "Beginning execution of main()"; #endif return 0; } 運(yùn)行結(jié)果為:Beginning execution of main() Press any key to continue
更一般的情況是,#define語句是包含在一個(gè)特定的頭文件中。 比如,新建頭文件head.h,在文件中加入代碼:
#ifndef DEBUG #define DEBUG #endif
而在define.cpp源文件中,代碼修改如下: #include "iostream.h" #include "head.h" int main(){ #ifdef DEBUG cout<< "Beginning execution of main()"; #endif return 0; } 運(yùn)行結(jié)果如下:Beginning execution of main() Press any key to continue 結(jié)論:通過使用#ifdef指示符,我們可以區(qū)隔一些與特定頭文件、程序庫和其他文件版本有關(guān)的代碼。
|