Why it's valid to include a header file twice in c++?

后端 未结 7 877
迷失自我
迷失自我 2021-02-09 17:25
#include \"DLLDefines.h\"
#include \"DLLDefines.h\"

The above actually passed compilation, but why?

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 18:04

    It's called an include guard.

    #ifndef GRANDFATHER_H
    #define GRANDFATHER_H
    
    struct foo {
        int member;
    };
    
    #endif
    

    Quote from Wikipedia:

    In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive. The addition of #include guards to a header file is one way to make that file idempotent.

    See link above for more information.

提交回复
热议问题