C ++:在小项目的头文件中编写整个类的缺点? [英] C++: Drawbacks to writing whole classes in Header files for small projects?

查看:165
本文介绍了C ++:在小项目的头文件中编写整个类的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个风格问题...

Just a style question...

我是一个低调独立的游戏开发者,由我自己开发,我开发的是我被告知是一个在我的标题中写整个类的习惯。我知道.h / .cpp文件组合的一些好处是它们允许代码分裂为编译块,不需要重新编译,只要它们保持不变。

I'm a lowly indie game dev working by myself, and I developed what I have been told is a 'bad' habit of writing whole classes in my headers. Some of the benefits I know .h/.cpp file combos have are they allow code to be split into compilation chunks that won't need recompiled so long as they remain unchanged. And allows for splitting interface from implementation.

然而,这些事情对我来说都没有什么好处,因为我倾向于把我的实现放在我可以轻松改进它,改变它,读它。我的编译时间是即时的(2-4秒,15如果我更新SFML或Box2D到最新版本,他们需要重新编译)

However, neither of those things are of any benefit to me, since I tend to favour having my implementation in a spot where I can easily improve it, change it, read it. And my compile times are nigh instantaneous (2-4 seconds, 15 if I updated SFML or Box2D to their latest versions and they need recompiled too)

这样的编码已经节省了我一个非常显着的时间量,我认为,因为有较少的文件,我的代码感觉不到压倒性对我。

Coding like this has been saving me a very noticeable amount of time I think, and since there are less files, my code feels less 'overwhelming' to me.

但是鉴于这一点,一般来说,对于一个小项目的每个file.h设置,有没有任何令人信服的理由来遵循file.cpp

But in light of that, and in general, is there any compelling reason to follow the "file.cpp" for every "file.h" setup for a small project where compile time and interface/implementation separation are not priorities?

推荐答案


有没有任何令人信服的理由对于编译时间和接口/实现分离不是优先级的小项目,每个file.h设置都应该使用file.cpp?

is there any compelling reason to follow the "file.cpp" for every "file.h" setup for a small project where compile time and interface/implementation separation are not priorities?

不;在头文件中定义类和函数没有什么问题,特别是在编译时不成问题的小项目中。

Nope; there's nothing wrong with defining classes and functions in header files, especially not in small projects where compile times aren't a concern.

对于值得的,我的当前, - 进步爱好项目有33个头文件和一个.cpp文件(不包括单元测试)。这主要是因为一切都是一个模板。

For what it's worth, my current, in-progress hobby project has 33 header files and a single .cpp file (not including unit tests). That is largely due to just about everything being a template, though.

如果你有一个巨大的软件项目,或者如果你需要封装你的代码到一个库,模块化,那么从头文件中分离代码可能是有意义的。如果你想隐藏一些实现细节(例如,如果你有一些丑陋的标题,你不想包括在项目的其他地方 - 例如WinAPI标题),那么将代码分割成一个单独的源文件是有意义的隐藏这些细节。否则,它可能只是很多的工作,不是很多的收益。

If you have a huge software project or if you need to encapsulate your code into a library and actually need the modularity, then it might make sense to split code out of a header file. If you want to hide some implementation detail (e.g., if you have some ugly header that you don't want to include elsewhere in your project--WinAPI headers, for example), then it makes sense to split code into a separate source file to hide those details. Otherwise, it may just be a lot of work for not a lot of gain.

这篇关于C ++:在小项目的头文件中编写整个类的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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