C / C ++前向声明与包含 [英] C/C++ Forward declaration vs. Include

查看:144
本文介绍了C / C ++前向声明与包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含某个文件时发生了什么,以及当您转发声明某个函数/类时发生了什么?如果两个文件包含相同的文件,第一个成功读取所有函数,第二个将失败,但仍然可以使用函数。



当我向前声明一些函数?这个功能现在保存了吗,我可以在任何地方使用它,或者它只为同一个文件知道?那么为什么包含两个文件(包括守卫文件)的原因会起作用?



我可以将每个东西都包含在主文件中,而且不会再打扰吗?



编辑:

为什么cpp文件应该包含它们的头文件?如果我不包含它们?

解决方案


当你包含一些文件和当你转发声明一些函数/类时发生了什么?


当你包含一个文件时,它的内容会被复制并粘贴预处理器的包含来源。当你转发声明一个函数/类时,你声明了一个不完整的类型,让其余的翻译单元知道一个具有该名称的函数/类存在并在允许不完整声明的上下文中使它可用。


如果两个文件包含相同的文件,第一个成功读取所有函数,第二个文件将失败,但仍然能够使用函数


如果包含的文件包含正确的包括警卫,在同一翻译单位中的第二次包含将实际上是空白。如果两个不同的源文件包含相同的头文件,则完整内容将包含在这两个文件中。


功能?这个功能现在保存了吗,我可以在任何地方使用它,或者它只为同一个文件知道?那么为什么包含两个文件(包括守卫文件)的原因会起作用?

该函数只能在翻译单元中使用,包含前向声明。通常,每个源文件(.cpp)是一个不同的翻译单元,宏定义(标题守护的那些)以及声明/定义在该翻译单元中是有效的。头文件防止相同的头文件在同一翻译单元中被多次包含,以防止出现多个声明错误。


What is happening when you include some file and what is happening when you forward declare some function/class? If two files include the same file will the first one success to read all the function the second will fail but still be able to use the functions??

What happens when I forward declare some function? Is this function now "saved" and I can use it anywhere or it's known only for the same file? then why two files with include(to a file with guards) will work?

Can I just include every thing in the main and won't bother any longer?

EDIT:

And why the cpp files should include their headers?? What If i won't include them?

解决方案

What is happening when you include some file and what is happening when you forward declare some function/class?

When you include a file, its contents get "copy and pasted" into the inclusion source by the preprocessor. When you forward declare a function/class you are declaring an incomplete type, letting the rest of the translation unit know that a function/class with that name exist and making it usable within context where an incomplete declaration is allowed.

If two files include the same file will the first one success to read all the function the second will fail but still be able to use the functions??

If the included file includes proper include guards, the second inclusion within the same translation unit will be effectively a no-op. If two different source files include that same header file, the full content will be included in both files.

What happens when I forward declare some function? Is this function now "saved" and I can use it anywhere or it's known only for the same file? then why two files with include(to a file with guards) will work?

The function can only be used within the translation unit that contains the forward declaration. Generally each source file (.cpp) is a different translation unit, macro definitions (those of the header guards) as well as declarations/definitions are valid within that translation unit. Header guards prevent the same header file from being included more than once within the same translation unit, to prevent multiple declaration errors.

这篇关于C / C ++前向声明与包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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