简体   繁体   English

c ++中的class(cpp文件和h文件)

[英]class (cpp file & h file) in c++

After creating and definition class (in "h file"). 创建和定义类后(在“h文件”中)。 How do I decide to create (or not) "cpp file" (only for the class) in addition to "h file" (that belonging to the class)? 除了“h file”(属于该类)之外,我如何决定创建(或不创建)“cpp文件”(仅适用于该类)?

Here is a small example to get you going. 这是一个让你前进的小例子。


this is Foo's header file. 这是Foo的头文件。 let's call it "foo.h" 我们称之为“foo.h”

#pragma once
#ifndef FOO_H
#define FOO_H

class Foo{
public:
    void function();
protected:
private:
};
#endif

This is Foo's source file. 这是Foo的源文件。 Let's call it "foo.cpp" 我们称之为“foo.cpp”

#include "foo.h"
void Foo::function(){
    // ... implement ...
    return;
}

compiling them together, we can create an object file. 将它们编译在一起,我们就可以创建一个目标文件。 We'll call it "foo.o" You can use your class in a program provided that you link "foo.o". 我们称之为“foo.o”。如果您链接“foo.o”,您可以在程序中使用您的课程。
Example: 例:

#include "foo.h"
int main(){

    Foo foo;
    foo.function();

    return 0;
}

An h file is a descriptor file, that describes the signature of your functions/classes, so that other classes in other cpp files may use it. h文件是描述符文件,它描述了函数/类的签名,因此其他cpp文件中的其他类可以使用它。

You need to think of an h file as a contract. 您需要将h文件视为合同。 You are declaring an obligation. 你是在宣布一项义务。

Later on, when you decide to implement the cpp, you are fulfilling the obligation. 稍后,当您决定实施cpp时,您将履行义务。

Other classes/cpp files can rely on your obligation alone, assuming that you will also implement the obligation in a cpp. 其他类/ cpp文件可以单独依赖您的义务,假设您还将在cpp中实现义务。

For example: 例如:

  1. You create an .h file "myClassA.h" and declare a class called myClassA with a member method called myClassA.SayHello() 您创建.h文件“myClassA.h”并使用名为myClassA.SayHello()的成员方法声明一个名为myClassA的类
  2. You include myClassA.h in another class you create myClassB.cpp, that way myClassB knows that myClassA has a method called SayHello() and it can call it. 你将myClassA.h包含在你创建myClassB.cpp的另一个类中,这样myClassB知道myClassA有一个名为SayHello()的方法,它可以调用它。
  3. If you do not include myClassA.h and you try to call myClassA.SayHello() inside myClassB.cpp, you'll get an error from your compiler, as myClassB does not "know" of myClassA. 如果你不包含myClassA.h并且你试图在myClassB.cpp中调用myClassA.SayHello(),那么你的编译器会收到一个错误,因为myClassB并不“知道”myClassA。
  4. If you do include the h file but did not bother to actually create and implement myClassA in myClassA.cpp, you will get a compilation error, since no implementation was found. 如果你确实包含了h文件,但没有在myClassA.cpp中实际创建和实现myClassA,那么你将得到一个编译错误,因为没有找到任何实现。

The best practice is to separate the header and implementation files so you define the class inside the header file .h and implement it inside the .cpp file, this will help you to trace and debugging the errors and shows a clean code , 最佳实践是分离头文件和实现文件,以便在头文件.h中定义类并在.cpp文件中实现它,这将帮助您跟踪和调试错误并显示干净的代码,

Just note in the templates classes it have to be defined in a separate header file to keep your code structured well and clean by separating templates from normal classes 请注意,在模板类中,必须在单独的头文件中定义它,以便通过将模板与普通类分离来保持代码结构良好和干净

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

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