[c++] 文件包含

  • 当一个类用到另一个类时,有两种包含方式,在.h中包含和在.cpp中包含
  • 用到公共类库时,在.h文件中包含(公共类库可视为不变的)

 

 

 

  • 用到项目开发过程中自己或同事写的类时,在.cpp文件中包含(可能根据需求有变化)
  • If a Package’s implementation depends on another Package, include the other’s header file in the implementation file, not the header.
  • 比如A用到了B,现在A.h中声明class B;,再在A.cpp中include "B.h",这样,即使以后B发生变化,A.h也不会变,用到A的其他类也不会受影响,从而避免了编译依赖

 

 

 

  • 举例,上图是在.h中包含,下图是在.cpp中包含,可以看出如果都通过.h包含,会形成比较长的依赖链,即编译依赖,而在.cpp中包含,各个.h文件间是并列关系,不会形成依赖

参考:

在.h文件中包含头文件和在.cpp文件中包含头文件的区别

https://blog.csdn.net/xueruifan/article/details/50569639

#include和直接写class加类名的区别

https://blog.csdn.net/CC_20171030/article/details/78557723

posted @ 2019-11-25 23:10  cxc1357  阅读(223)  评论(0编辑  收藏  举报