#include anywhere

后端 未结 7 1128
心在旅途
心在旅途 2020-12-15 08:55

Is the #include meant to be used for headers only or is it simply a mechanical \"inject this code here\" that can be used anywhere in the code?

7条回答
  •  清歌不尽
    2020-12-15 09:49

    include is handled by the preprocessor and is a mechanism to inject code. There are no restrictions on the file being included or where this #include is placed in your code (thought it should be in its own line). As long as the file specified can be found by the preprocessor it will import its contents into the current file.

    Conventionally you do this for header files. I've seen this being done with cpp files during template instantiation (with proper #ifdef so you don't include it multiple times causing multiple symbol definition error).

    If you have a long constant, you can do this for other file types as well. (Though there are better ways of handling long string constants)

提交回复
热议问题