简体   繁体   English

为什么要使用 header 文件和 C++ 中的声明?

[英]Why to use header files with declarations in C++?

This answer says we need header files for declarations of other functions placed somewhere else, for example in .lib or .dll or .cpp files. 这个答案说我们需要 header 文件来声明放置在其他地方的其他函数,例如在.lib.dll.cpp文件中。

My question is, why do we really need to use declarations in header files at all?我的问题是,为什么我们真的需要在 header 文件中使用声明? Why can't we just use definitions straight away?为什么我们不能直接使用定义? For example, place a definition instead of a declaration and address to it directly.例如,放置一个定义而不是一个声明并直接指向它。 To a beginner, all this declaration stuff seems to be sort of a redundant work.对于初学者来说,所有这些声明的东西似乎是一种多余的工作。

A few reasons come to mind:想到几个原因:

  1. C++ being an old language, it is sensitive to the ordering of code. C++ 是一门古老的语言,它对代码的顺序很敏感。 Things must be declared before they can be used, and header files are a convenient and consistent way to do this.必须先声明事物才能使用它们,而 header 文件是一种方便且一致的方式来执行此操作。 For example, this example would work in most modern languages, but not in C++:例如,此示例适用于大多数现代语言,但不适用于 C++:
int main() {
    foo();  // error: 'foo' has not been declared
    return 0;
}

void foo() { /* ... */ }
  1. It separates the interface from the implementation.它将接口与实现分开。 This means you can push out updates to a library's implementation, and as long as none of the interfaces have changed, clients do not need to re-compile their code.这意味着您可以将更新推送到库的实现,只要没有任何接口发生更改,客户端就不需要重新编译他们的代码。 This also means that you can have multiple implementations corresponding to a single interface - for example, maybe you want to write a filesystem library, but the implementation of that library will have to look significantly different on Linux than it does on Windows.这也意味着您可以拥有对应于单个接口的多个实现 - 例如,也许您想编写一个文件系统库,但该库的实现在 Linux 上的外观必须与在 Windows 上的外观大不相同。 Both of these implementations can share the same header, making the implementation details "opaque" to users.这两种实现可以共享同一个 header,使得实现细节对用户“不透明”。

Bear in mind that all definitions are declarations in C++ (but not all declarations are definitions).请记住,所有定义都是C++ 中的声明(但并非所有声明都是定义)。

Non- inline functions are usually declared but not defined in a header to avoid breaking the one-definition rule (and linking problems) when that header is included in multiple sources.inline函数通常在 header 中声明但未定义,以避免在 header 包含在多个源中时破坏单一定义规则(和链接问题)。

Declarations (not definitions) of some class / struct types are needed in headers to break circular dependencies (which causes diagnosable errors and prevents compilation).头文件中需要一些class / struct类型的声明(不是定义)以打破循环依赖关系(这会导致可诊断的错误并阻止编译)。

These types of concerns rarely arise in "toy" problems used in programming courses and ivory towers, but are critically important in real-world development.这些类型的问题很少出现在编程课程和象牙塔中使用的“玩具”问题中,但在现实世界的开发中却至关重要。 For example, in toy problems, a header file might only be included in a single source file - so the concerns of breaking the one-definition rule don't arise.例如,在玩具问题中,header 文件可能只包含在单个源文件中 - 因此不会出现违反单一定义规则的问题。 But real-world projects typically include headers in multiple source files - so are affected by problems that arise when definitions are inappropriately placed into header files.但实际项目通常在多个源文件中包含标头 - 因此会受到定义不恰当地放入 header 文件时出现的问题的影响。

In short, the approaches used in small learning exercises (which cause students to wonder why there is any need to put declarations into headers rather than definitions) don't scale to real-world development.简而言之,小型学习练习中使用的方法(这使学生想知道为什么需要将声明放入标题而不是定义中)并不能适应实际开发。

Some types of definitions (templates, inline functions, and class / struct definitions) are routinely placed into headers.某些类型的定义(模板、内联函数和class / struct定义)通常放置在标题中。 That doesn't mean that all definitions should be placed in headers.这并不意味着所有定义都应该放在标题中。

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

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