简体   繁体   English

C ++ One Header Multiple Sources

[英]C++ One Header Multiple Sources

I have a large class Foo 1 : 我有一个大班Foo 1

class Foo {
public:
    void apples1();
    void apples2();
    void apples3();

    void oranges1();
    void oranges2();
    void oranges3();
}

Splitting the class is not an option 2 , but the foo.cpp file has grown rather large. foo.cpp分类不是一个选项2 ,但foo.cpp文件已经变得相当大。 Are there any major design flaws to keeping the definition of the class in foo.h and splitting the implementation of the functions into foo_apples.cpp and foo_oranges.cpp . foo.h保留类的定义并将函数的实现分成foo_apples.cppfoo_oranges.cpp是否存在任何主要的设计缺陷。

The goal here is purely readability and organization for myself and other developers working on the system that includes this class. 这里的目标纯粹是可读性和组织,我自己和其他开发人员在包含这个类的系统上工作。


1 "Large" means some 4000 lines, not machine-generated. 1 “大”意味着大约4000行,而不是机器生成的。
2 Why? 2为什么? Well, apples and oranges are actually categories of algorithms that operate on graphs but use each other quite extensively. 嗯, applesoranges实际上是在图表上运行但相互广泛使用的算法类别。 They can be separated but due to the research nature of the work, I'm constantly rewiring the way each algorithm works which I found for me does not (in the early stage) jive well with the classic OOP principles. 它们可以分开,但由于工作的研究性质,我不断重新布线每种算法的工作方式,我发现对我来说(在早期阶段)与经典的OOP原则并不相符。

Are there any major design flaws to keeping the definition of the class in foo.h and splitting the implementation of the functions into foo_apples.cpp and foo_oranges.cpp. 在foo.h中保留类的定义并将函数的实现分成foo_apples.cpp和foo_oranges.cpp是否存在任何主要的设计缺陷。

to pick nits: Are there any major design flaws to keeping the declaration of the class in foo.h and splitting the definitions of the methods into foo_apples.cpp and foo_oranges.cpp. 选择nits:在foo.h中保留类的声明并将方法定义分成foo_apples.cpp和foo_oranges.cpp是否存在任何主要的设计缺陷。

1) apples and oranges may use the same private programs. 1)苹果和橘子可以使用相同的私人程序。 an example of this would be implementation found in an anonymous namespace. 一个例子是在匿名命名空间中找到的实现。

in that case, one requirement would be to ensure your static data is not multiply defined. 在这种情况下,一个要求是确保您的静态数据不是多次定义的。 inline functions are not really a problem if they do not use static data (although their definitions may be multiply exported). 如果内联函数不使用静态数据(尽管它们的定义可以多次导出),它们并不是真正的问题。

to overcome those problems, you may then be inclined to utilise storage in the class -- which could introduce dependencies by increasing of data/types which would have otherwise been hidden. 为了克服这些问题,您可能会倾向于利用类中的存储 - 这可能会通过增加本来隐藏的数据/类型来引入依赖关系。 in either event, it can increase complexity or force you to write your program differently. 在任何一种情况下,它都会增加复杂性或迫使您以不同的方式编写程序。

2) it increases complexity of static initialization. 2)它增加了静态初始化的复杂性。

3) it increases compile times 3)它增加了编译时间

the alternative i use (which btw many devs detest) in really large programs is to create a collection of exported local headers . 我在真正大的程序中使用的替代方案(对于许多开发人员来说很讨厌)是创建导出的本地标头的集合。 these headers are visible only to the package/library. 这些标头只对包/库可见。 in your example, it can be illustrated by creating the following headers: Foo.static.exported.hpp (if needed) + Foo.private.exported.hpp (if needed) + Foo.apples.exported.hpp + Foo.oranges.exported.hpp . 在您的示例中,可以通过创建以下标Foo.static.exported.hpp说明: Foo.static.exported.hpp (如果需要)+ Foo.private.exported.hpp (如果需要)+ Foo.apples.exported.hpp + Foo.oranges.exported.hpp

then you would write Foo.cpp like so: 然后你会像这样写Foo.cpp:

#include "DEPENDENCIES.hpp"
#include "Foo.static.exported.hpp" /* if needed */
#include "Foo.private.exported.hpp" /* if needed */
#include "Foo.apples.exported.hpp"
#include "Foo.oranges.exported.hpp"

/* no definitions here */

you can easily adjust how those files are divided based on your needs. 您可以根据需要轻松调整这些文件的分配方式。 if you write your programs using c++ conventions, there are rarely collisions across huge TUs. 如果你使用c ++约定编写你的程序,那么巨大的TU很少会发生冲突。 if you write like a C programmer (lots of globals, preprocessor abuse, low warning levels and free declarations), then this approach will expose a lot of issues you probably won't care to correct. 如果你像C程序员一样编写(许多全局变量,预处理程序滥用,低警告级别和自由声明),那么这种方法将暴露许多你可能不会纠正的问题。

From a technical standpoint, there is no penalty to doing this at all, but I have never seen it done in practice. 从技术的角度来看,完全没有惩罚,但我从未在实践中看到它。 This is simply a issue of style, and in that spirit, if it helps you to better read the class, then you would be doing yourself a disservice by not using multiple source files. 这只是一个风格问题,并且在这种精神上,如果它能帮助你更好地阅读课程,那么你就不会因为不使用多个源文件而对自己造成伤害。

edit: Adding to that though, are you physically scrolling through your source, like, with your middle mouse wheel? 编辑:除此之外,您是否使用鼠标中键滚动浏览源代码? As someone else already mentioned, IDE's almost universally let you right click on a function declaration, and go to the definition. 正如其他人已经提到的那样,IDE几乎普遍允许您右键单击函数声明,然后转到定义。 And even if that's not the case for your IDE, and you use notepad or something, it will at least have ctrl+f. 即使您的IDE不是这种情况,并且您使用记事本或其他东西,它至少会有ctrl + f。 I would be lost without find and replace. 没有找到和替换我会迷路。

Yes, you can define the class in one header file and split the function implementations accross multiple source files. 是的,您可以在一个头文件中定义类,并在多个源文件中拆分函数实现。 It is not usually the common practice but yes it will work and there will be no overheads. 这通常不是常见的做法,但是它会起作用并且不会产生任何管理费用。

If the aim to do so, is just plain readability, then perhaps it is not a good idea to do so, because it is not so common practice to have class function definitions accross multipls source files and might just confuse someone. 如果这样做的目的只是简单的可读性,那么也许这样做并不是一个好主意,因为将类函数定义与多个源文件相比并不常见,并且可能只会让某些人感到困惑。

Actually i don't see any reasons to split implementation because other developers should work with the interface, but not the implementation. 实际上我没有看到任何拆分实现的理由,因为其他开发人员应该使用接口,而不是实现。

Also any normal IDE provide an easy ability to jump from function declaration to it's defenition. 此外,任何普通的IDE都可以轻松地从函数声明跳转到它的defenition。 So there is no reason to search the function implementations manually. 因此没有理由手动搜索功能实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM