including a header file twice in c++

前端 未结 5 1723
失恋的感觉
失恋的感觉 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条回答
  •  一向
    一向 (楼主)
    2021-01-01 17:45

    It simply gets skipped over, due to preprocessor code along the following lines:

    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    
    
    
    #endif
    

    So if you include twice, then MY_HEADER_H is already defined and everything between the #ifndef and #endif is skipped by the preprocessor.

提交回复
热议问题