抽象类和接口在C ++ [英] Abstract Class vs Interface in C++

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

问题描述


可能重复:

这是关于C ++的一般问题。如你所知,C ++中的 interface 抽象类没有明显的区别,与Java和C#不同。什么时候更喜欢在C ++中使用接口而不是抽象类

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组件只导出接口(即,指向v表的指针,即指向一组函数指针的指针)。这有助于定义 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's),如本文中所述,它更好导出接口(成熟的方法)而不是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天全站免登陆