多继承和通用程序 [英] Multiple inheritance and generic program

查看:144
本文介绍了多继承和通用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Andrei Alex的Modern C ++ Design:Generic Programming and Design Patterns Applied,刚刚开始。在第6页,它有多重继承的以下批评:

I am reading "Modern C++ Design: Generic Programming and Design Patterns Applied" by Andrei Alex., just started. At page 6, it has the following critcism of multiple inheritance:


使用多重继承组装单独的特性的问题如下: / p>

The problems with assembling separate features by using multiple inheritance are as follows:


  1. ...

  2. 类型信息。有足够的类型
    信息来执行他们的任务。例如,假设您尝试通过从 DeepCopy
    基类派生来尝试
    实现深度复制您的智能指针类。 DeepCopy 有什么接口?它必须创建一个尚不知道的
    类型的对象。

  1. ...
  2. Type information. The base classes do not have enough type information to carry out their tasks. For example, imagine you try to implement deep copy for your smart pointer class by deriving from a DeepCopy base class. What interface would DeepCopy have? It must create objects of a type it doesn't know yet.


我想知道这个特定的批判是否有缺陷。

I am wondering if this particular critique is flawed.

接口驱动的设计通常有基类为纯虚拟类,子类实现接口。以DeepCopy为例,我将这样做:

Interface driven design normally has base class a pure virtual class and the child class implements the interfaces. Take DeepCopy as example, I would do this:

struct DeepCopy 
{
   virtual void copy(DeepCopy *src) = 0;
};

class MyClass : public DeepCopy, public AnotherIntf
{
   public:
      virtual void copy(DeepCopy *src); 
};

在这个例子中,MyClass是实现者,它是一个真正的类。

In this example, MyClass is the implementer and it is a real class.

也许我错过了Andrei在这里的批评。

Maybe I miss the point of Andrei's critique here.

你觉得怎么样?

推荐答案

我的理解是,Alexandrescu在这里讲的是一个外部的复制工具,而不是一个需要修改正在复制的类。对于给定类实现深层复制的正确方法可能因类的实现而有所不同,因此没有办法向外部复制设施提供类型信息,您无法选择正确的方法。

It was my understanding that Alexandrescu was speaking of an external copying facility here, not an intrusive one that requires modification of the classes being copied. The proper way to implement deep copy for a given class may differ based on the class's implementation, so without a way to provide type information to the external copying facility you can't select the proper approach.

深度复制可能不是一个很好的例子 - 他的章节广义函数,智能指针和多个分派是我会考虑更好的他的基于策略的设计的现实世界的例子。

Deep copy may not be a great example here -- his chapters on generalized functors, smart pointers, and multiple dispatch are what I would consider better real-world examples of his policy-based design.

这篇关于多继承和通用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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