简体   繁体   English

一个让C++代码分割成.h和.cpp文件对程序员透明的工具

[英]A tool to make the division of C++ code into .h and .cpp files transparent to the programmer

C++ was the the first language I've learnt so dividing source code into.h and.cpp files seemed obvious - but having learnt C# and Java they now appear to me terribly clumsy. C++ 是我学习的第一种语言,因此将源代码分成 .h 和 .cpp 文件似乎很明显 - 但在学习了 C# 和 Java 之后,它们现在在我看来非常笨拙。 They might have been useful back in the 60s, maybe still even in 80s, but in the era of modern tools, such as IDEs with section folding, and documentation generators, they seem obsolete.它们可能在 60 年代很有用,甚至在 80 年代可能仍然有用,但在现代工具时代,例如带有部分折叠的 IDE 和文档生成器,它们似乎已经过时了。

Anyway, my question is: is there a tool which makes the presence of these two kind of files transparent to the programmer?无论如何,我的问题是:是否有一种工具可以使这两种文件的存在对程序员透明? For example by letting the coder write the definition of a method seemingly in the header file but actually saving it to the.cpp file?比如让coder把一个方法的定义写在header文件里,实际保存到.cpp文件里?

(I know one can try to write a C++ program solely in header files but AFAIK this is not considered best practice, makes the program build even longer and makes it virtually impossible for two classes to reference each other.) (我知道可以尝试仅在 header 个文件中编写一个 C++ 程序,但据我所知,这不是最佳实践,它会使程序构建时间更长,并且两个类几乎不可能相互引用。)

The discussion that I am seeing in the question, comments and the comments to the other answer seem to focus on the textual representation of the components.我在问题、评论和对其他答案的评论中看到的讨论似乎集中在组件的文本表示上。 From the point of view of plain text, it makes sense to remove the headers altogether.从纯文本的角度来看,完全删除标题是有意义的。

On the other hand, there is a second level to the separation of headers and cpp files, which is separating the interface from the implementation, and in doing so, removing implementation details from the interface.另一方面,头文件和 cpp 文件的分离还有第二个层次,即将接口与实现分开,并在这样做时从接口中删除实现细节。

This happens in different ways, in the simplest level how you implement a particular piece of functionality is irrelevant to the users of your component[*].这以不同的方式发生,在最简单的层面上,您如何实现特定功能与组件的用户无关[*]。 In many cases, you can have more types and functions in the.cpp file that are used just as details of implementation.在许多情况下,您可以在 .cpp 文件中包含更多类型和函数,这些类型和函数仅用作实现细节。 Additionally, if you decide to implement a particular functionality directly or by depending on another library is a detail of implementation and should not leak to your users.此外,如果您决定直接或依赖于另一个库来实现特定功能,则属于实现细节,不应泄露给您的用户。

That separation might or might not be easy to implement in a tool that managed the separation of files automatically, and cannot be done by those that like the use of header only libraries.在自动管理文件分离的工具中实现这种分离可能容易也可能不容易,并且不能由那些只喜欢使用header库的人来完成。

Where some of you are claiming that there is no point in having to Go to definition , and would like to see the code altogether, what I see is the flexibility of deciding what parts of your component are details that should not be known by users.你们中的一些人声称不必Go 来定义是没有意义的,并且希望看到完整的代码,我看到的是可以灵活地决定组件的哪些部分是用户不应该知道的细节 In many IDEs (and, heck, even in vim) there is a single keystroke combination that will take you from one to the other.在许多 IDE 中(甚至在 vim 中)都有一个单一的击键组合,可以让你从一个到另一个。 In IDEs you can refactor the signature of the function and have the IDE apply the changes to both the header and the implementation (sometimes even to the uses)...在 IDE 中,您可以重构function 的签名,并让 IDE 将更改应用于 header 和实现(有时甚至应用于用途)...

If you were to have a tool provide a unified view of both the header and implementation, it would probably be much harder to make the tool knowledgable of what parts of the code you are writing are part of the interface or the implementation, and the decisions that the tool might have could have an impact on the generated program.如果你有一个工具提供 header 和实现的统一视图,那么让工具了解你正在编写的代码的哪些部分是接口或实现的一部分以及决策可能会更难该工具可能会对生成的程序产生影响。

The separate compilation model has its disadvantages but also advantages, and I feel that the discussion that is being held here is just scratching the surface of deeper design decisions.单独编译 model 有其缺点但也有优点,我觉得这里进行的讨论只是触及更深层次设计决策的表面。

[*] There seems to be quite a few people that believe that each class should have its own header and.cpp files, and I disagree, each header represents a component that might be a single class, or multiple classes and free functions the separation of code in files is part of the design , and in a single component you might have one or more public types together with possibly none or more internal types. [*] 似乎有不少人认为每个 class 应该有自己的 header 和.cpp 文件,我不同意,每个 header 代表一个组件,可能是单个 class,或者多个类和自由函数分离文件中的代码是设计的一部分,在单个组件中,您可能有一个或多个公共类型,可能没有或有多个内部类型。

I don't know of any that makes the division into source/header completely transparent.我不知道有什么能使源代码/标题的划分完全透明。

I do, however, know of some that make it considerably easier to handle.但是,我确实知道一些可以使它更容易处理的方法。 For example, Visual Assist X will let write your entire class in a header, then select member functions, and move them to a source file (ie, a.cpp file).例如,Visual Assist X 会让您将整个 class 写入 header,然后是 select 成员函数,并将它们移动到源文件(即 a.cpp 文件)。

That's not a complete cure (by any means), but it can/does make them much more bearable anyway.这不是完全治愈(无论如何),但无论如何它可以/确实让他们更容易忍受。

Personally, I think it would be interesting to get rid of files completely, and instead use something like a database directly -- ie, you have a database of functions, where the source code to that function is one column, object code another, internal compiler information about how to use it yet another, and so on.就个人而言,我认为完全摆脱文件并直接使用数据库之类的东西会很有趣——也就是说,你有一个函数数据库,其中 function 的源代码是一列,object 代码是另一列,内部关于如何使用它的编译器信息还有另一个,等等。 This would also make integrating version control pretty straightforward (basically just a stored transaction log).这也将使集成版本控制变得非常简单(基本上只是一个存储的事务日志)。

I, for one, like the header files in C++, because they can serve as a "table of contents" of a sort, which gives you a quick overview of a class. This is, of course, assuming that you keep your headers short, and put the implementation of most functions into the.cpp.我,例如,喜欢 C++ 中的 header 文件,因为它们可以作为某种“目录”,让您快速了解 class。当然,这是假设您的标题很短,并将大部分功能的实现放到.cpp中。

Also, you can get plugins for Visual Studio that will move the implementation of a member function from the header into a.cpp for you.此外,您可以获得 Visual Studio 的插件,它将成员 function 的实现从 header 移动到 a.cpp 中。 That, plus the "go to definition" and "go to declaration" commands in Visual Studio make headers very helpful for navigating a large code base.那加上 Visual Studio 中的“转到定义”和“转到声明”命令,使标头对于导航大型代码库非常有用。

Headers are pretty fundamental to the current compilation model of C++. I think there are things that would be a bit difficult to express without exposing the programmer directly to the header/cpp split.标头对于 C++ 的当前编译 model 来说是非常基础的。我认为如果不让程序员直接接触标头/cpp 拆分,有些东西会有点难以表达。 For example, in certain circumstances there's a significant difference between:例如,在某些情况下,两者之间存在显着差异:

// header
class Foo {
    Foo() = default;
}

and

// header
class Foo {
    Foo();
};

// cpp
Foo::Foo() = default;

Despite that I think it would be worthwhile to think about improving the compilation model or making it easier to deal with.尽管如此,我认为考虑改进编译 model 或使其更易于处理是值得的。

For example, I can imagine a 'code bubbles' style IDE where the programmer writes definitions in code bubbles, and then groups bubbles together into translation units.例如,我可以想象一个“代码气泡”样式 IDE,程序员在代码气泡中编写定义,然后将气泡组合成翻译单元。 The programmer would mark definitions as 'exported' to make them available to other TUs and for each TU he'd select exported items from other units to import.程序员会将定义标记为“已导出”,以便其他 TU 可以使用它们,并且对于每个 TU,他将从其他单元导出项目 select 以导入。 And all the while the IDE is maintaining it's own code bubble representation of the program, and when you build it generates the actual cpp files necessary for passing off to the compiler, creating forward declarations, etc. as needed. IDE 一直在维护它自己的程序代码气泡表示,当您构建它时,它会生成传递给编译器所需的实际 cpp 文件,根据需要创建前向声明等。 The cpp files would just be an intermediate build product. cpp 文件只是一个中间构建产品。

There would be subtleties like the one I showed above though, and I'm not sure how the IDE I described would deal with that.虽然会有像我上面展示的那样的微妙之处,但我不确定我描述的 IDE 将如何处理它。

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

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