看一个例子:
#include <iostream> using namespace std; #ifndef max_length #define max_length 50 #endif #ifndef max_length #define max_length 100 #endif int main() { cout << max_length << endl; return 0; }那么输出结果显然是50了,由于max_length已经定义为50了,定义为100的部分没有执行。
#include <iostream> using namespace std; #include "test1.h" #include "test.h" int main() { test t; return 0; }test.h:
#include <iostream> using namespace std; #ifndef _TEST_H #define _TEST_H class test { public: test() { cout << "Test has been constructed." << endl; } }; #endiftest1.h:
#include <iostream> using namespace std; #ifndef _TEST_H #define _TEST_H class test { public: test() { cout << "Test 1 has been constructed." << endl; } }; #endif注意在test_main中,我们先#include "test1.h",那么在下一个#include "test.h"中由于已经定义了_TEST_H标识,就不会再定义test.h里面的test类,注意一般标识的写法是下划线+头文件名大写+下划线+H,如_TEST_H,那么输出显然是:
假如调换#include
"test1.h"和#include "test.h"的顺序:
但是乱打的错误必须在标识内部:
还有一些奇怪的东西:
原文:http://blog.csdn.net/u012925008/article/details/44575635