天天看點

ifndef 和 #pragma once 差別

#ifndef   是為了防止一個源檔案多次include同一個頭檔案。

No, it's not the same.

If your compiler supports "#pragma once",

It will ignore the rest of the header file when it finishes reading this line.

But if you use "#ifndef xxx", "#define xxx", "...", "#endif",

the compiler will go on parsing this file because it thinks that

  there may be some more lines of code after "#endif",

it dare not to drop the rest of the file.

As you see,

if the compiler supports "#pragma once",

using "#pragma once" will accelerate the compiling process.

So people usually write header file like this:

#ifndef XXX

#define XXX

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER

...

#endif // XXX

繼續閱讀