为什么C ++需要一个单独的头文件? [英] Why does C++ need a separate header file?

查看:183
本文介绍了为什么C ++需要一个单独的头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有真正理解为什么C ++需要一个单独的头文件,具有与.cpp文件中相同的功能。它使得创建类和重构非常困难,并且它向项目添加了不必要的文件。



C ++在1998年被批准,所以为什么是这样的呢?设计这种方式?具有单独的头文件有什么优点?






h2>

当所有我包含的是.h文件时,编译器如何找到.cpp文件中的代码?是否假定.cpp文件与.h文件的名称相同,或者它实际上是透过目录树中的所有文件?

解决方案

你似乎要求从声明中分离定义,尽管头文件还有其他用途。



答案是C ++不会需要这个。如果你标记一切内联(对于在类定义中定义的成员函数,这是自动的),那么不需要分离。



您可以 分隔的原因是:


  1. 提高构建时间。

  2. 无需定义源代码即可链接。

  3. <

    如果你更常见的问题是,为什么C ++与Java不一样? ,那么我要问,你为什么要写C ++而不是Java? ;-p



    更重要的是,原因是C ++编译器不能到达另一个翻译单元,并且不知道如何使用它的符号,在javac可以和做的方式。需要头文件来向编译器声明它期望在链接时可用的。



    所以 #include 是一个直接的文本替换。如果你定义头文件中的所有内容,预处理器最终会在你的项目中创建一个庞大的拷贝和粘贴每个源文件,并将它们送入编译器。事实上,C ++标准在1998年被批准,这与C ++无关,事实上C ++的编译环境与C的编译环境非常接近。



    转换我的注释以回答后续问题:


    编译器如何找到.cpp文件中的代码


    它不会,至少不是在编译使用头文件的代码时。你所链接的函数甚至不需要被编写,不用担心编译器知道它们会在什么 .cpp 文件。所有的调用代码需要知道在编译的时候是表示在函数声明中。在链接时,您将提供一个 .o 文件的列表,或静态或动态库,并且标题实际上是一个承诺函数的定义将在那里


    I've never really understood why C++ needs a separate header file with the same functions as in the .cpp file. It makes creating classes and refactoring them very difficult, and it adds unnecessary files to the project. And then there is the problem with having to include header files, but having to explicitly check if it has already been included.

    C++ was ratified in 1998, so why is it designed this way? What advantages does having a separate header file have?


    Follow up question:

    How does the compiler find the .cpp file with the code in it, when all I include is the .h file? Does it assume that the .cpp file has the same name as the .h file, or does it actually look through all the files in the directory tree?

    解决方案

    You seem to be asking about separating definitions from declarations, although there are other uses for header files.

    The answer is that C++ doesn't "need" this. If you mark everything inline (which is automatic anyway for member functions defined in a class definition), then there is no need for the separation. You can just define everything in the header files.

    The reasons you might want to separate are:

    1. To improve build times.
    2. To link against code without having the source for the definitions.
    3. To avoid marking everything "inline".

    If your more general question is, "why isn't C++ identical to Java?", then I have to ask, "why are you writing C++ instead of Java?" ;-p

    More seriously, though, the reason is that the C++ compiler can't just reach into another translation unit and figure out how to use its symbols, in the way that javac can and does. The header file is needed to declare to the compiler what it can expect to be available at link time.

    So #include is a straight textual substitution. If you define everything in header files, the preprocessor ends up creating an enormous copy and paste of every source file in your project, and feeding that into the compiler. The fact that the C++ standard was ratified in 1998 has nothing to do with this, it's the fact that the compilation environment for C++ is based so closely on that of C.

    Converting my comments to answer your follow-up question:

    How does the compiler find the .cpp file with the code in it

    It doesn't, at least not at the time it compiles the code that used the header file. The functions you're linking against don't even need to have been written yet, never mind the compiler knowing what .cpp file they'll be in. Everything the calling code needs to know at compile time is expressed in the function declaration. At link time you will provide a list of .o files, or static or dynamic libraries, and the header in effect is a promise that the definitions of the functions will be in there somewhere.

    这篇关于为什么C ++需要一个单独的头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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