在头文件中实现类的优点和缺点是什么? [英] What are the advantages and disadvantages of implementing classes in header files?

查看:232
本文介绍了在头文件中实现类的优点和缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢DRY的概念(不要重复自己[oops]),但是C ++的头文件概念违背了这种编程规则。在头中定义类成员是否有任何缺点?如果它是正确的做模板,为什么不为正常类?

I love the concept of DRY (don't repeat yourself [oops]), yet C++'s concept of header files goes against this rule of programming. Is there any drawback to defining a class member entirely in the header? If it's right to do for templates, why not for normal classes? I have some ideas for drawbacks and benefits, but what are yours?

推荐答案

将所有内容放在头文件中可能具有的优点:

Possible advantages of putting everything in header files:


  • 减少冗余(导致更容易的更改,更容易重构等)


可能的缺点包括:将所有内容放在头文件中:

Possible disadvantages of putting everything in header files:


  • 更长的编译/链接周期


  • 可能导致难以解决的循环依赖

  • 许多内联会增加可执行文件大小

  • 阻止共享库/ DLL的二进制兼容性

  • 颠覆喜欢使用C ++的传统方式的同事

  • Longer compile/link cycles
  • Loss of separation of interface and implementation
  • Could lead to hard-to-resolve circular dependencies
  • Lots of inlining could increase executable size
  • Prevents binary compatibility of shared libraries/DLLs
  • Upsets co-workers who prefer the traditional ways of using C++

这篇关于在头文件中实现类的优点和缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆