简体   繁体   English

C ++类中的标题和代码

[英]Header and Code in C++ classes

I just started on a few C++ tutorials, and I have run into something that I just can't seem to make much sense of. 我刚开始学习一些C ++教程,但是遇到了一些我似乎不太理解的东西。

In C++ it seems people are using a code file and a header file, for me this just seem inconvinient. 在C ++中,似乎人们正在使用代码文件和头文件,对我而言,这似乎并不方便。 Why would I want to swap around between two files just to write a simple getter method. 为什么我只想编写一个简单的getter方法就想在两个文件之间交换。

Is it considered the "correct" way to use headers in C++? 是否认为在C ++中使用标头的“正确”方式? Or is it just the tutorial I have picked up that uses this? 还是只是我选择的教程使用了这个?

I get the idea of splitting code to make it look more clean, but is it good for anything else other than that? 我想到了拆分代码以使其看起来更干净的想法,但是除此以外的其他功能是否有用?

Thanks in advance. 提前致谢。

There are some reasons for using hpp(header)- and cpp(code)-files. 使用hpp(header)-和cpp(code)-文件有一些原因。 One of them is the following: A library (dll- or so-file) cannot be "used" like a jar-file in java. 其中之一是:库(dll文件或so​​-文件)不能像Java中的jar文件一样“使用”。 If you write a library, you have to provide declarations of the classes, methos,... in form of a hpp-file. 如果编写库,则必须以hpp文件的形式提供类,方法等的声明。

A header file in c++ stores alot of information, if c++ have been made using every single "header" file in c++ in each program you make, when you then write a function from iostream for example, the program will go through every single header file just to find the right header file. c ++中的头文件可存储大量信息,如果使用所创建的每个程序中的c ++中的每个“头”文件来制作c ++,则例如当您随后从iostream编写函数时,程序将遍历每个头文件只是找到正确的头文件。 so instead they made the #inlcude function in c++, so you could specify where your functions are from. 因此,他们改为在c ++中创建了#inlcude函数,因此您可以指定函数的来源。

And when you create a program you could make own header files, so the code is more nicely set up. 而且,当您创建程序时,可以创建自己的头文件,因此可以更好地设置代码。 and then instead of having to make alot of lines of code in one main source file, you could import others. 然后不必导入一个主要源文件中的许多代码行,而可以导入其他代码。 like if you are making a game, one header file for Animals and in that header file you have a Class for Cats, and one for dogs. 例如,如果您要制作游戏,则一个动物文件的头文件,而在该头文件中,您有一个用于猫的类,一个用于狗的类。 having a more clean code. 拥有更干净的代码。

Think about using the class you wrote in other files. 考虑使用您在其他文件中编写的类。 If you had the class definition in a separate file, you could help the compiler to figure out how to use the class by including the header file in places where you are planning to use this code. 如果您在单独的文件中包含类定义,则可以通过在计划使用此代码的位置包含头文件来帮助编译器弄清楚如何使用该类。

The compiler only needs to know whether you are using the classes right(it does not care about how to run it, until linking), therefore all you need to give the compiler is the declaration of the class(header file), to do the error checking. 编译器只需要知道您是否正确使用了类(在链接之前它并不关心如何运行它),因此,您需要给编译器的是类(头文件)的声明,以进行编译。错误检查。 When you say "include" , the preprocessor just copies and pastes the header file contents into the new file, so that the new file now knows how to use the class you wrote. 当您说"include" ,预处理器只是将头文件的内容复制并粘贴到新文件中,以便新文件现在知道如何使用您编写的类。

In C/C++, headers are used to share the class structure (among other things) between classes. 在C / C ++中,标头用于在类之间共享类结构(除其他外)。

so one can use 所以一个人可以使用

include "classFOO.h" 包括“ classFOO.h”

in classBAR.h (or classBAR.cpp) and use classFOO. 在classBAR.h(或classBAR.cpp)中使用classFOO。

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

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