(C ++).h文件中的整个类? [英] (C++) Whole class in .h file?

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

问题描述

如果我创建一个具有小功能的类,不做太多,只是把它们全部放入头文件可以接受吗?所以对于一个特定的类,只有.h没有.cpp可以用它。

解决方案

。它一定会编译。但是,如果它使代码组织更清洁,那么这可以是好的。大多数模板定义已经是这样的必要性,所以你不会做任何闻所未闻的事情。可能有一些缺点的类依赖于其他类虽然。如果你最终必须将整个定义包含在使用该类的其他文件中,那么与只有一个简短的类声明相比,可能会产生额外的编译时间。





如果您可以获得一份副本, c ++编程语言(和许多其他书)有关于源代码组织的详细部分以及将代码分为.h和.cpp文件的具体好处。 / p>

If I am creating a class with small functions that don't do much, is it acceptable to just put them all into the header file? So for a particular class, it's only just the .h with no .cpp to go with it.

解决方案

Yes, that's acceptable. It will certainly compile. But also, if it makes the code organization cleaner, then that can be good. Most template definitions are already like this out of necessity, so you aren't doing anything unheard of. There can be some drawbacks of that class relies on other classes though. If you end up having to include the whole definition in other files that use the class, it may incur additional compile time compared to only having a brief class declaration.

You can measure your compilation times if this seems to be a real concern.

If you can get a copy, The c++ Programming Language (and many other books) have a detailed section about source code organization and the specific benefits of separating code into .h and .cpp files.

这篇关于(C ++).h文件中的整个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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