Best practice for including from include files

后端 未结 6 1134
谎友^
谎友^ 2021-01-21 02:05

I was wondering if there is some pro and contra having include statements directly in the include files as opposed to have them in the source file.

Personally I like to

6条回答
  •  礼貌的吻别
    2021-01-21 02:14

    I think second approach is a better one just because of following reason.

    1. when you have a function template in your header file.

      class myclass
      {
          template
          void method(T& a)
          {
             ...
          }
      }
      

    And you don't want to use it in the source file for myclass.cxx. But you want to use it in xyz.cxx, if you go with your first approach then you will end up in including all files that are required for myclass.cxx, which is of no use for xyz.cxx.

    That is all what I think of now the difference. So I would say one should go with second approach as it makes your code each to maintain in future.

提交回复
热议问题