C++ 抽象类是否需要 .cpp 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14001356/
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 17:52:41  来源:igfitidea点击:

Do I need to have a .cpp file for an abstract class?

c++visual-studio-2010

提问by Lemon Juice

First, I am new to C++. I open a header file for each and every C++ class. Now I am in a need of creating an abstract class. Following is my code

首先,我是 C++ 的新手。我为每个 C++ 类打开一个头文件。现在我需要创建一个抽象类。以下是我的代码

Magic.h

魔法.h

#pragma once
class Magic
{
public:
    Magic(void);
    ~Magic(void);
    virtual void display()=0;
};

Magic.cpp

魔术文件

#include "Magic.h"


Magic::Magic(void)
{
}


Magic::~Magic(void)
{
}

Now, as you know I can't add following to the cpp file.

现在,如您所知,我无法将以下内容添加到 cpp 文件中。

Magic::display()
{
}

So, do I really need a .cpp file for an Abstract class? Or else, am I incorrectly calling display()in .cpp file?

那么,我真的需要一个抽象类的 .cpp 文件吗?或者,我是否错误地调用display()了 .cpp 文件?

采纳答案by Mat

You don't needan implementation file. Just define all the required members inline (and don't define the pure virtual ones if you don't need to).

不需要实现文件。只需内联定义所有必需的成员(如果不需要,不要定义纯虚拟成员)。

class Magic
{
public:
    Magic(void) {};
    ~Magic(void) {};
    virtual void display()=0;
};

回答by Bhargav Bhat

If you're following the 2-files-per-classand one-class-in-each-set-of-filesconventions, it would be better to have a .cppfile even if you're writing an abstract class.

如果您遵循2-files-per-classone-class-in-each-set-of-files约定,.cpp即使您正在编写抽象类,也最好有一个文件。

There are a few other advantages apart from maintaining consistency: having a .cppor implementation file gives you room to expand in the future, if you were to add a non-virtual common behaviour to your family of classes, the base class is the right place to add it. Also minor edits in the .cppfile will result in far lesser compilation time than those in a .hwhich are included from multiple places.

除了保持一致性之外,还有其他一些优势:拥有一个.cpp或实现文件为您提供了将来扩展的空间,如果您要向您的类家族添加非虚拟的通用行为,则基类是正确的地方添加它。此外,.cpp文件中的微小编辑将导致编译时间比.h从多个地方包含的文件中的编辑时间短得多。

Conventions aside, you can very well place a class (abstract or not) in just a single .hfile. Libraries such as the STL do this all the time.

撇开约定不谈,您可以很好地将类(抽象或非抽象)放在单个.h文件中。诸如 STL 之类的库一直都在这样做。

回答by Anonymous Coward

You don't need a source file for abstract classes (i.e interface).

您不需要抽象类(即接口)的源文件。

Furthermore, if you have a virtual method of any kind, you should add a virtualdestructor as well (or make the destructor protected). Also you can simply let the compiler generate a default constructor for you. If you want a pure virtual function (that the derived class MUST override) you should not add a function body so leave the void Magic::display() {}away. And since you have to provide a body for the destructor, you can define it inline:

此外,如果您有任何类型的虚拟方法,您还应该添加一个虚拟析构函数(或使析构函数受保护)。您也可以简单地让编译器为您生成默认构造函数。如果你想要一个纯虚函数(派生类必须覆盖),你不应该添加一个函数体,所以离开void Magic::display() {}。由于您必须为析构函数提供一个主体,因此您可以内联定义它:

struct Magic {
    // use default constructor
    virtual void display() =0; // pure virtual function
    virtual ~Magic(); // virtual destructor
};

inline Magic::~Magic()
{
}

or even simpler:

甚至更简单:

#pragma once

struct Magic {
    virtual void display() =0;
    virtual ~Magic() {}
};

回答by billz

No, you can add a function definitionfor pure virtual function but normally you don't need to provide function definition for pure virtual function. Note, you are missing function return type for Magic::display(), should be:

不,您可以definition为纯虚函数添加函数,但通常您不需要为纯虚函数提供函数定义。请注意,您缺少 , 的函数返回类型Magic::display(),应该是:

void Magic::display()
{
}

回答by SomeWittyUsername

If your class contains only virtualmethods, it actually defines an interface. An interface by nature represents a contract between 2 entities, without any implementation dependency. Therefore as a good design practice not only you don't need the .cppfile, it's better without it.

如果你的类只包含虚拟方法,它实际上定义了一个接口。一个接口本质上代表了 2 个实体之间的契约,没有任何实现依赖。因此,作为一个好的设计实践,您不仅不需要该.cpp文件,而且没有它更好。

Since you're using VS, you can also use the MS extension __interfacefor interfaces instead of classes with pure virtual methods (beware, it's not portable).

由于您使用的是 VS,您还可以将 MS 扩展__interface用于接口而不是具有纯虚拟方法的类(请注意,它不可移植)。