简体   繁体   English

真的需要将 ah 和 a.cpp 文件中的 c++ 结构分开吗?

[英]Is really required to separate c++ constructions in a .h and a .cpp files?

Well, I'm getting in C++ universe and this doubt came over.好吧,我进入了 C++ 宇宙,这个疑问就解决了。

It's too boring to have 2 files for each meaning-unit I choose to include in my program.对于我选择包含在我的程序中的每个含义单元都有 2 个文件太无聊了。 I know I can have multiple classes in the same (pair of) archive(s), however I would like to clarify if really there's no way to write just one file, instead of a .h and a .cpp ones.我知道我可以在同一个(一对)档案中拥有多个类,但是我想澄清一下是否真的没有办法只写一个文件,而不是一个.h和一个.cpp文件。

I found some other answers (like this , that and that other ) there are actually pretty explicative, but a quite older too.我发现了一些其他的答案(比如这个那个那个其他),实际上非常有解释性,但也很老。 So hopping the language have got some improvement I came to ask:因此,跳跃语言得到了一些改进,我来问:

Is there some compilation option , any another alternative extension , or whatever, that allows me to write just one file?是否有一些编译选项、任何其他替代扩展名或其他任何允许我只编写一个文件的东西?

I appreciate it!我很感激!

Okay, you need to understand what is going on.好的,您需要了解发生了什么。 This isn't Java.这不是 Java。

The.h file is the definition of your class. .h 文件是您的 class 的定义 It's just an include file that can be used other places so those other places understand your class.它只是一个可以在其他地方使用的包含文件,因此其他地方可以理解您的 class。 Now, you CAN actually do the constructor inline, like this:现在,您实际上可以内联构造构造函数,如下所示:

public:
     Foo() { ... your code here ... }

This is perfectly legal.这是完全合法的。 But the problem with this is simple.但是这个问题很简单。 Everywhere you hit this constructor, the compiler has to insert that code inline.在您点击此构造函数的任何地方,编译器都必须内联插入该代码。 This leads to lots of the same code everywhere you create a new Foo.这会导致您在任何地方创建一个新的 Foo 时都会出现大量相同的代码。

If you put the code in your.cpp file, and then compile it, you get ao file.如果你把代码放在你的.cpp 文件中,然后编译它,你会得到一个ao 文件。 That.o file includes a single copy of your constructor, and that's what gets called everywhere you create a Foo. That.o 文件包含您的构造函数的单个副本,这就是在您创建 Foo 的任何地方都会调用的内容。

So separating the definition from the code results in smaller programs.因此,将定义与代码分开会产生更小的程序。 That's less important nowadays than it used to be.如今,这已不如从前那么重要了。

This is the nature of C++, and you should accept it.这是 C++ 的本质,你应该接受它。

The.h is an include file, used in other places. .h 是一个包含文件,用于其他地方。 The.cpp is the implementation. .cpp 是实现。 It's not really onerous, once you grow accustomed.一旦你习惯了,这并不繁重。

You have to understand that C++ is a compiled language.您必须了解 C++ 是一种编译语言。 When you compile a library, for example, the library contains machine-specific code.例如,当您编译一个库时,该库包含特定于机器的代码。 If you want to write a program that uses that library, your program has to be able to see function and class definitions to properly link that library.如果您想编写一个使用该库的程序,您的程序必须能够看到 function 和 class 定义才能正确链接该库。 On the other hand, it is absolutely possible to write your entire program in header files -- indeed, the term header-only library exists to describe libraries that have no pre-compiled machine code.另一方面,绝对有可能在 header 文件中编写整个程序——事实上,存在术语仅头文件库来描述没有预编译机器代码的库。 That means the responsibility of compiling it falls to you.这意味着编译它的责任落在你身上。 You'll likely have longer compile times, and because of this, very large libraries are almost exclusively pre-compiled into binaries that are platform-specific (in the absence of a set of binaries for you machine, you must compile from source and link against the result).您可能会有更长的编译时间,因此,非常大的库几乎完全预编译为特定于平台的二进制文件(在您的机器没有一组二进制文件的情况下,您必须从源代码和链接编译反对结果)。 In theory, one could rewrite the C++ spec in such a way that only one file was necessary, but then those files would need to be present within any project that incorporates that library.理论上,可以重写 C++ 规范,只需要一个文件,但是这些文件需要存在于包含该库的任何项目中。 For very large libraries, this can be a pain -- why include the full source of some engine when you could include just the definitions necessary to link into the binaries?对于非常大的库,这可能会很痛苦——当您可以只包含链接到二进制文件所需的定义时,为什么还要包含某些引擎的完整源代码? This provides the added advantage of obfuscating the algorithms and implementation details from the client program.这提供了从客户端程序混淆算法和实现细节的额外优势。 C++ is not an interpreted programming language -- it's important to think about it from the compiler's perspective. C++ 不是一种解释型编程语言——从编译器的角度考虑这一点很重要。

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

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