为什么需要h文件? [英] why is h file needed?

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

问题描述

大家好

我想知道为什么需要h文件?

为什么不能用C文件写一切?

谢谢

解决方案

Montezuma'的女儿写道:


我想知道为什么h文件是否需要?

为什么不能用C文件编写所有内容?



它可以,也可以。在大多数情况下,标题的存在('h'代表''标题'')只是为了创造

方便。例如,如果你想在你的类中使用
多个.C文件(.C代表Unix文件扩展名

常用于C ++源文件,而不是C语言),你需要

编译器可以看到该类的定义

编译aC和bC时(和所有其他文件是

在您的项目中单独编译)。你可以在每个文件中复制

代码,或者你可以简单地将代码放在一些

其他文件中,称之为''myclass.h'并执行源代码

包含使用预处理器指令#include。在

这种情况​​下,如果你需要编辑你的类,你可以在

标题中,一个地方,而不是通过所有定义
$ b所有.C文件中同一类的$ b。


或者我是否误解了你的困惑?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


1月8日下午1:13,Montezuma的女儿 < Urania.m ... @ gmail.com>

写道:


嗨所有

我想知道为什么需要h文件?

为什么不能用C文件写一切?

谢谢



我们不讨论C文件,源文件通常是* .cpp文件在C +

+


它不需要,取决于什么你做。标题用于保持孤立的单独声明。这是* .h文件的作用。如果您选择

来声明源文件中的所有内容(即:* .cpp)那么当您意识到找不到时,就不会感到惊讶您在一个混乱的声明中或在多个翻译单元中重复使用相同的声明。


我应该在10个不同的项目中需要给定的类型而且我已经

有一个自定义类型来完成这项工作,我很难重写

类型,只需复制type.h和type.cpp。 #include" type.h" (完成)


所以这真的是一个问题,你是否想要以艰难的方式去做,或者简单的方式。我赞成后者。


On 2008-01-08 14:15:06 -0500,Salt_Peter< pj ***** @ yahoo.comsaid:


1月8日下午1点13分,Montezuma'的女儿 < Urania.m ... @ gmail.com>

写道:


>大家好
我想知道为什么h文件需要?
为什么一切都不能写成C文件?
谢谢



我们不讨论C文件,源文件通常是* .cpp文件在C +

+



有趣的是,许多编译器将.C文件视为C ++。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:一个教程和参考的作者

www.petebecker.com/tr1book

>


Hi All
I was wondering why h file ia needed?
why can''t everything be written in C file?
thanks

解决方案

Montezuma''s Daughter wrote:

I was wondering why h file ia needed?
why can''t everything be written in C file?

It can, and it does, in a way. In most cases the existence
of headers (the ''h'' stands for ''header'') is just to create
convenience. For example, if you want to use your class in
more than one .C file (.C stands for the Unix file extension
commonly used for C++ source file, not C language), you need
the definition of that class to be visible to the compiler
when it compiles a.C and b.C (and all other files that are
compiled separately in your project). You can duplicate the
code in each file, or you can simply place the code in some
other file, call it ''myclass.h'' and perform source code
inclusion using the preprocessor directive #include. In
that case, if you need to edit your class, you do it in the
header, one place, instead of going through all definitions
of the same class in all .C files.

Or did I misinterpret your confusion?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jan 8, 1:13 pm, "Montezuma''s Daughter" <Urania.m...@gmail.com>
wrote:

Hi All
I was wondering why h file ia needed?
why can''t everything be written in C file?
thanks

We don''t disscuss C files, source files are typically *.cpp files in C+
+

Its not needed, depending on what you do. Headers are used to keep
seperate declarations isolated. Thats what *.h files do. If you choose
to declare everything in your source files (ie: *.cpp) then don''t be
surprised when you realize that you can''t find your declarations in a
mess or reuse the same declarations in multiple translation units.

Should i require a given type in 10 different projects and i already
have a custom type that does the job, i''ld be nuts to rewrite the
types, just copy type.h and type.cpp over. #include "type.h" (done)

So its really a question of whether you want to do it the hard way, or
the easy way. I pprefer the latter.


On 2008-01-08 14:15:06 -0500, Salt_Peter <pj*****@yahoo.comsaid:

On Jan 8, 1:13 pm, "Montezuma''s Daughter" <Urania.m...@gmail.com>
wrote:

>Hi All
I was wondering why h file ia needed?
why can''t everything be written in C file?
thanks


We don''t disscuss C files, source files are typically *.cpp files in C+
+

Funny thing, many compilers treat .C files as C++.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


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

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