Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?

后端 未结 5 1549
無奈伤痛
無奈伤痛 2021-02-14 20:33

I\'m fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This

5条回答
  •  借酒劲吻你
    2021-02-14 20:51

    Like Travis said, you shouldn't have using statements in a header file because that means they will be included in all the translation units that include that header file, which can cause confusing issues.

    If I only require the functionality from a header file in a cpp file, I only include it in that cpp file. It's good practice for larger projects because it means less work for the compiler. Also, wherever possible, I use forward declarations in the headers instead of includes (and again, include the headers in the cpp file).

提交回复
热议问题