简体   繁体   English

头文件基本上是预写的类吗? C ++

[英]Are header files basically prewritten classes? c++

Exactly as it states in the title, are header files that you use the #include for basically prewritten/pre coded classes filled with methods and variable that you can use once you include it into your script. 标题中的内容与标题文件中所声明的完全一样,您可以将#include用于基本上预写/预编码的类,这些类中填充有方法和变量,一旦将其包含在脚本中就可以使用。 Also how can one go about making one to the point where you can simply use header files that you created for your own benefit? 另外,如何才能使您仅使用为自己的利益而创建的头文件呢? A simple explanation will do. 一个简单的解释就可以了。

Yes and no... it depends on many things. 是和否...这取决于很多事情。

An include directive -as for the standard C++ is- can be two things: 与标准C ++一样,include指令可以是两件事:

#inlclude "filename"

and

#include <header>

The first one literally means "keep the text contained in "filename" and paste it here . So yes, the file can contain prewritten classes (but not necessaraily have to) 第一个字面意思是“保留“文件名”中包含的文本并将其粘贴到此处 。是的,该文件可以包含预写的类(但不必如此)

The second one means "let this source to use the facilities the standard defines in its <header> section. As per the standard definition goes, this is not necessarily meant to be a file, although all the implementation I'm today aware of in fact do: header is a file-name and <> and "" simply change the compiler search order. 第二个意思是“让该源使用标准在其<header>部分中定义的功能。按照标准定义,虽然我今天知道的所有实现都不一定是文件。事实确实如此: 标头是文件名,而<>""只是更改了编译器的搜索顺序。

You can -at this point- cut a "very long Cpp source" into smaller self-contained chunks and come to a cpp file that is nothing more than a sequence of #include and a main() . 此时,您可以将“非常长的Cpp源”切成较小的独立块,并生成一个cpp文件,该文件不过是#includemain()的序列。

But that's not all. 但这还不是全部。

A C++ program is not necessarily made by a single cpp file (a cpp file plus everything it includes either directly or indirectly is called "translation unit"). C ++程序不一定由单个cpp文件制作(cpp文件加上它直接或间接包含的所有内容都称为“翻译单元”)。

In fact compilation does not produce programs, but "object files" (the term is "ancient operating system terminology", and have nothing to do with object-oriented programming) that a "linker" puts together. 实际上,编译不是生成程序,而是由“链接器”组合在一起的“目标文件”(该术语是“古老的操作系统术语”,与面向对象的编程无关)。

Because a C++ function calling another does not need -to be translated- to know how the other function is translated, but only how its parameters and return values have to be passed and taken, This makes you able to write the other function body in another independently compiled source and let the header to contain only its declaration (not definition). 因为调用另一个函数的C ++函数不需要(要翻译)知道另一个函数是如何翻译的,而只需知道其参数和返回值是如何传递和采用的,这使您能够在另一个函数体中编写另一个函数体。独立编译的源,并让标头仅包含其声明(不包含定义)。

For example you can have 例如你可以有

// fooclass.h
#ifndef FOOCLASS_H
#define FOOCLASS_H
class foo
{
public:
   void hello();
   void bye();
};
#endif // FOOCLASS_H

,

// out.h
#ifndef OUT_H
#define OUT_H 
#include <string>
void printout(const std::string& s);
#endif // OUT_H

,

//main.cpp
#include "fooclass.h"
int main()
{
   foo afoo;
   afoo.hello();
   afoo.bye();
}

,

// fooclass.cpp
#include "fooclass.h"
#include "out.h"

void foo::hello()
{ printout("hello"); }

void foo::bye()
{ printout("bye"); }

,

// out.cpp
#include "out.h"
#include <iostream>

void printout(const std::string& s)
{ std::cout << "- " << s << " -" << std::endl; }

The entire program can be compiled (for example with GCC) as 整个程序可以编译为(例如,使用GCC)为

g++ main.cpp fooclass.cpp out.cpp

or in separated step as 或在单独的步骤中

g++ -c main.cpp
g++ -c fooclass.cpp
g++ -c out.cpp
g++ main.o fooclass.o out.o

(if you use MS compilers, you'll most likely do (如果您使用MS编译器,则很可能会

cl /c main.cpp
cl /c fooclass.cpp
cl /c out.cpp
link main.obj fooclass.obj out.obj

)

Only the last step makes up the program (note that main.cpp will never know about the printout function!). 只有最后一步构成了程序(请注意,main.cpp将永远不会知道printout功能!)。 The previous three are required only for files that have been changed, thus on big projects can speed up compilation and distribute tasks to different teams. 前三个仅对于已更改的文件是必需的,因此在大型项目上可以加快编译速度并将任务分配给不同的团队。 (there are utiities like make , that allow to automate this thing. (有诸如make类的实用工具可以自动执行此操作。

There is an exception to all that, that nowadays is more and more important: templates . 所有这些都有一个例外,即当今越来越重要: 模板 Since they are not implemented as "code to be translated" but as "code to be expanded", they can be translated only when what they have to expand is known. 由于它们不是实现为“要翻译的代码”而是实现为“要扩展的代码”,因此只有在知道必须扩展的内容时才能进行翻译。

template<class T>
T max(const T& a, const T& b)
{ return (a<b)? b: a; }

The meaning of (a<b) depends own what the type T actually is. (a<b)的含义取决于类型T实际上是什么。 And since it is know only when max is called, max can be expanded as max<int> or max<double> or max<string> only when used. 而且由于它是知道的,只有当max叫,最大可扩展为max<int>max<double>max<string>时才使用。

For this reason, templates are not normally handled in independent translation units and library offering template classes or functions do that by just providing collection of headers containing all the code, with no source to compile separately. 因此,通常不会在独立的翻译单元中处理模板,提供模板类或函数的库仅通过提供包含所有代码的标头集合来做到这一点,而没有单独编译的源。

are header files that you use the #include for basically prewritten/pre coded classes filled with methods and variable that you can use once you include it into your script? 您是否使用#include头文件来填充基本的预编码/预编码类,这些类中填充了将方法和变量包含在脚本中后就可以使用的方法和变量?

That's the general concept. 这是一般概念。 In C++, not everything needs to be in a class, so there can be non-member functions, types, preprocessor definitions, variables etc. too. 在C ++中,并非所有内容都需要在类中,因此也可以有非成员函数,类型,预处理器定义,变量等。 Further, some headers declare variables, types and/or functions but do not define them - that means you can make limited use of them and generally have to present the matching implementation object at link or run-time. 此外,某些标头声明变量,类型和/或函数,但未定义它们-这意味着您可以有限地使用它们,并且通常必须在链接或运行时提供匹配的实现对象。

Also how can one go about making one to the point where you can simply use header files that you created for your own benefit? 另外,如何才能使您仅使用为自己的利益而创建的头文件呢?

There's no particular magic about it (though there're linking issues above to learn about if you put implementation out-of-line), but to start with just throw some code in a file: 并没有什么特别的魔术(尽管上面有一些链接问题可以了解您是否将实现置于离线状态),但首先要在文件中添加一些代码:

#pragma once
...

...then #include it from another file. ...然后#include另一个文件中的内容。 If the including file is in directory "X", then you can include headers in directory "X" using eg #include "the_header.h" - with double-quoted header filename - even if the include directory is not specified to the compiler as a command line argument. 如果包含文件位于目录“ X”中,则可以使用#include "the_header.h"在目录“ X”中包含标头-带有双引号的标头文件名-即使未向编译器指定包含目录也是如此命令行参数。

If you have something in your header that's declared but not defined, then you will want a matching .cc/.cpp file with the definition, which you should compile into an object and link with the code using the library. 如果标头中有已声明但未定义的内容,则将需要一个具有定义的匹配.cc / .cpp文件,您应将该文件编译为一个对象,并使用该库与代码链接。 For example: 例如:

mylib.h
    #pragma one
    int fn();  // declared but not defined

mylib.cc
    #include "mylib.h"
    int fn() { return 42; }   // matching definition

cc -c mylib      // compile mylib.o

app.cc
    #include "mylib.h"
    int main()
    {
        return fn();
    }

cc -o app mylib.o

A common use for header files in c++ is to define methods or classes without having to implement them. c ++中头文件的常见用法是无需实现即可定义方法或类。 It's defining an interface, or telling other parts of your program how it can interact with them. 它定义了一个接口,或者告诉程序的其他部分如何与之交互。 It lets the files doing the including know what they need to know about those classes/methods without telling everything they don't. 它使包含在内的文件知道它们需要了解的有关这些类/方法的信息,而无需告诉他们所有不需要的信息。 In the case of methods, you know there's a method doSomethingCool, and you know how to interact with it. 对于方法,您知道有一个方法doSomethingCool,并且您知道如何与之交互。 As long as you know that you can generally just #include whatever header file doSomethingCool is defined in, and then the linker does its magic at compile time and finds where you called doSomethingCool and "hooks it up" so to speak to the actual implementation code, which you may write say in doSomethingCool.cpp. 只要你知道你通常可以只#包括任何头文件doSomethingCool 定义的,然后链接程序它的魔力在编译的时候,发现在那里你叫doSomethingCool和“挂钩起来”可以这么说,以实际执行代码,您可以在doSomethingCool.cpp中写出来。

Works with functions, classes and variables. 与函数,类和变量一起使用。

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

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