including a header file twice in c++

前端 未结 5 1609
失恋的感觉
失恋的感觉 2021-01-01 17:03

What happens if I include iostream or any other header file twice in my file? I know the compiler does not throw error.

Will the code gets added twice

5条回答
  •  -上瘾入骨i
    2021-01-01 17:30

    Header files are simple beasts. When you #include

    all that happens is that the contents of header basically get copy-pasted into the file. To stop headers from being included multiple times, include guards are used, which is why in most header files you'll see something akin to

    #ifndef SOME_HEADER_FILE_GUARD 
    #define SOME_HEADER_FILE_GUARD
    
    //Contents of Header
    
    #endif
    

提交回复
热议问题