where should “include” be put in C++

后端 未结 9 1995
生来不讨喜
生来不讨喜 2020-12-14 16:06

I\'m reading some c++ code and Notice that there are "#include" both in the header files and .cpp files . I guess if I move all the "#include" in the fi

9条回答
  •  -上瘾入骨i
    2020-12-14 16:14

    The include files in a header should only be those necessary to support that header. For example, if your header declares a vector, you should include vector, but there's no reason to include string. You should be able to have an empty program that only includes that single header file and will compile.

    Within the source code, you need includes for everything you call, of course. If none of your headers required iostream but you needed it for the actual source, it should be included separately.

    Include file pollution is, in my opinion, one of the worst forms of code rot.

    edit: Heh. Looks like the parser eats the > and < symbols.

提交回复
热议问题