C++ 中的抽象类与接口 [英] Abstract Class vs Interface in C++

查看:26
本文介绍了C++ 中的抽象类与接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 C++ 中声明接口?

这是一个关于 C++ 的一般问题.如您所知,与 Java 和 C# 不同,C++ 中的 interfaceabstract class 之间没有明显区别.什么时候在 C++ 中使用 interface 而不是 abstract class 更可取?你能举几个例子吗?

This is a general question about C++. As you know, there is no clear distinction between interface and abstract class in C++ unlike Java and C#. When would it be more preferrable to use an interface instead of an abstract class in C++? Could you give some examples?

推荐答案

我假设 interface 是指只有 纯虚 方法(即没有任何方法)的 C++ 类代码),而不是抽象类,你指的是一个带有可以被覆盖的虚拟方法的C++类,以及一些代码,但至少有一个纯虚拟方法使类不是可实例化.例如:

I assume that with interface you mean a C++ class with only pure virtual methods (i.e. without any code), instead with abstract class you mean a C++ class with virtual methods that can be overridden, and some code, but at least one pure virtual method that makes the class not instantiable. e.g.:

class MyInterface
{
public:
  // Empty virtual destructor for proper cleanup
  virtual ~MyInterface() {}

  virtual void Method1() = 0;
  virtual void Method2() = 0;
};


class MyAbstractClass
{
public:
  virtual ~MyAbstractClass();

  virtual void Method1();
  virtual void Method2();
  void Method3();

  virtual void Method4() = 0; // make MyAbstractClass not instantiable
};

在 Windows 编程中,接口COM的基础.实际上,COM 组件仅导出接口(即指向 v-tables 的指针,即指向函数指针集的指针).这有助于定义一个 ABI(应用程序二进制接口),使之成为可能,例如用 C++ 构建一个 COM 组件并在 Visual Basic 中使用它,或者用 C 构建一个 COM 组件并在 C++ 中使用它,或者用 Visual C++ 版本 X 构建一个 COM 组件并在 Visual C++ 版本 Y 中使用它.换句话说,通过接口,您可以在客户端代码和服务器代码之间实现高度解耦.

In Windows programming, interfaces are fundamental in COM. In fact, a COM component exports only interfaces (i.e. pointers to v-tables, i.e. pointers to set of function pointers). This helps defining an ABI (Application Binary Interface) that makes it possible to e.g. build a COM component in C++ and use it in Visual Basic, or build a COM component in C and use it in C++, or build a COM component with Visual C++ version X and use it with Visual C++ version Y. In other words, with interfaces you have high decoupling between client code and server code.

此外,当您想使用 C++ 面向对象的接口(而不是纯 C DLL)构建 DLL 时,如 这篇文章,最好是导出接口(成熟的方法")而不是C++类(这基本上就是COM所做的,但没有 COM 基础设施的负担).

Moreover, when you want to build DLL's with a C++ object-oriented interface (instead of pure C DLL's), as described in this article, it's better to export interfaces (the "mature approach") instead of C++ classes (this is basically what COM does, but without the burden of COM infrastructure).

如果我想定义一组可以用来对组件进行编程的规则,而不指定具体的特定行为,我会使用 接口.实现这个接口的类会自己提供一些具体的行为.

I'd use an interface if I want to define a set of rules using which a component can be programmed, without specifying a concrete particular behavior. Classes that implement this interface will provide some concrete behavior themselves.

相反,当我想提供一些默认的基础结构代码和行为时,我会使用抽象类,并使客户端代码可以从这个抽象派生类,使用一些自定义代码覆盖纯虚拟方法,并使用自定义代码完成此行为.以 OpenGL 应用程序的基础设施为例.您可以定义一个抽象类来初始化 OpenGL,设置窗口环境等,然后您可以从这个类派生并实现自定义代码,例如渲染过程和处理用户输入:

Instead, I'd use an abstract class when I want to provide some default infrastructure code and behavior, and make it possible to client code to derive from this abstract class, overriding the pure virtual methods with some custom code, and complete this behavior with custom code. Think for example of an infrastructure for an OpenGL application. You can define an abstract class that initializes OpenGL, sets up the window environment, etc. and then you can derive from this class and implement custom code for e.g. the rendering process and handling user input:

// Abstract class for an OpenGL app.
// Creates rendering window, initializes OpenGL; 
// client code must derive from it 
// and implement rendering and user input.
class OpenGLApp
{
public:
  OpenGLApp();
  virtual ~OpenGLApp();
  ...

  // Run the app    
  void Run();


  // <---- This behavior must be implemented by the client ---->

  // Rendering
  virtual void Render() = 0;

  // Handle user input
  // (returns false to quit, true to continue looping)
  virtual bool HandleInput() = 0;

  // <--------------------------------------------------------->


private:
  //
  // Some infrastructure code
  //
  ... 
  void CreateRenderingWindow();
  void CreateOpenGLContext();
  void SwapBuffers();
};


class MyOpenGLDemo : public OpenGLApp
{
public:
  MyOpenGLDemo();
  virtual ~MyOpenGLDemo();

  // Rendering
  virtual void Render();  // implements rendering code

  // Handle user input
  virtual bool HandleInput(); // implements user input handling


  //  ... some other stuff
};

这篇关于C++ 中的抽象类与接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆