C++ 将类代码分成头文件和 cpp 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9579930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:01:10  来源:igfitidea点击:

Separating class code into a header and cpp file

c++oopclass

提问by drdrdr

I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?

我对如何将一个简单类的实现和声明代码分离到一个新的头文件和 cpp 文件中感到困惑。例如,我将如何分离以下类的代码?

class A2DD
{
  private:
  int gx;
  int gy;

  public:
  A2DD(int x,int y)
  {
    gx = x;
    gy = y;
  }

  int getSum()
  {
    return gx + gy;
  }
};

回答by Ferenc Deak

The class declaration goes into the header file. It is important that you add the #ifndefinclude guards, or if you are on a MS platform you also can use #pragma once. Also I have omitted the private, by default C++ class members are private.

类声明进入头文件。添加#ifndef包含守卫很重要,或者如果您在 MS 平台上,您也可以使用#pragma once. 我也省略了私有,默认情况下 C++ 类成员是私有的。

// A2DD.h
#ifndef A2DD_H
#define A2DD_H

class A2DD
{
  int gx;
  int gy;

public:
  A2DD(int x,int y);
  int getSum();

};

#endif

and the implementation goes in the CPP file:

并且实现在 CPP 文件中:

// A2DD.cpp
#include "A2DD.h"

A2DD::A2DD(int x,int y)
{
  gx = x;
  gy = y;
}

int A2DD::getSum()
{
  return gx + gy;
}

回答by Nick

In general your .h contains the class defition, which is all your data and all your method declarations. Like this in your case:

通常,您的 .h 包含类定义,即您的所有数据和所有方法声明。在你的情况下是这样的:

A2DD.h:

class A2DD
{
  private:
  int gx;
  int gy;

  public:
  A2DD(int x,int y);    
  int getSum();
};

And then your .cpp contains the implementations of the methods like this:

然后你的 .cpp 包含这样的方法的实现:

A2DD.cpp:

A2DD::A2DD(int x,int y)
{
  gx = x;
  gy = y;
}

int A2DD::getSum()
{
  return gx + gy;
}

回答by j riv

It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.

重要的是要向在更广泛的方式研究该主题时遇到这个问题的读者指出,如果您只想将项目拆分为文件,则不需要接受的答案过程。仅当您需要单个类的多个实现时才需要它。如果每个类的实现是一个,那么每个类只需要一个头文件就足够了。

Hence, from the accepted answer's example only this part is needed:

因此,从接受的答案的例子中,只需要这部分:

#ifndef MYHEADER_H
#define MYHEADER_H

//Class goes here, full declaration AND implementation

#endif

The #ifndef etc. preprocessor definitions allow it to be used multiple times.

#ifndef 等预处理器定义允许它被多次使用。

PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".

附注。一旦您意识到 C/C++ 是“愚蠢的”并且#include 只是一种说“在此位置转储此文本”的方式,该主题就会变得更加清晰。

回答by Corbin

Basically a modified syntax of function declaration/definitions:

基本上是函数声明/定义的修改语法:

a2dd.h

a2dd.h

class A2DD
{
private:
  int gx;
  int gy;

public:
  A2DD(int x,int y);

  int getSum();
};

a2dd.cpp

a2dd.cpp

A2DD::A2DD(int x,int y)
{
  gx = x;
  gy = y;
}

int A2DD::getSum()
{
  return gx + gy;
}

回答by Yochai Timmer

A2DD.h

A2DD.h

class A2DD
{
  private:
  int gx;
  int gy;

  public:
  A2DD(int x,int y);

  int getSum();
};

A2DD.cpp

A2DD.cpp

  A2DD::A2DD(int x,int y)
  {
    gx = x;
    gy = y;
  }

  int A2DD::getSum()
  {
    return gx + gy;
  }

The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.

这个想法是在头文件中保留所有函数签名和成员。
这将允许其他项目文件查看类的外观,而无需知道实现。

And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.

除此之外,您还可以在实现中包含其他头文件而不是头文件。这很重要,因为头文件中包含的任何头文件都将包含(继承)在包含头文件的任何其他文件中。

回答by Mat

You leave the declarations in the header file:

您将声明保留在头文件中:

class A2DD
{
  private:
  int gx;
  int gy;

  public:
    A2DD(int x,int y); // leave the declarations here
    int getSum();
};

And put the definitions in the implementation file.

并将定义放在实现文件中。

A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
  gx = x;
  gy = y;
}

int A2DD::getSum()
{
  return gx + gy;
}

You could mix the two (leave getSum()definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.

您可以将两者混合使用(getSum()例如在标题中保留定义)。这很有用,因为它为编译器提供了更好的内联机会。但这也意味着更改实现(如果留在标题中)可能会触发包含标题的所有其他文件的重建。

Note that for templates, you need to keep it all in the headers.

请注意,对于模板,您需要将其全部保留在标题中。

回答by Ivaylo Strandjev

Usually you put only declarations and really short inline functions in the header file:

通常你只在头文件中放置声明和非常短的内联函数:

For instance:

例如:

class A {
 public:
  A(); // only declaration in the .h unless only a short initialization list is used.

  inline int GetA() const {
    return a_;
  }

  void DoSomethingCoplex(); // only declaration
  private:
   int a_;
 };

回答by Spyros Mourelatos

I won't refer too your example as it is quite simple for a general answer (for example it doesn't contain templated functions ,which force you to implement them on the header) , what I follow as a rule of thumb is the pimpl idiom

我不会太引用你的例子,因为它对于一般答案来说非常简单(例如它不包含模板化函数,这迫使你在标题上实现它们),我遵循的经验法则是pimpl成语

It has quite some benefits as you get faster compilation times and the syntactic sugar :

它有很多好处,因为您可以获得更快的编译时间和语法糖:

class->memberinstead of class.member

class->member代替 class.member

The only drawback is the extra pointer you pay.

唯一的缺点是您支付的额外指针。