继承与虚拟函数VS泛型编程 [英] Inheritance & virtual functions Vs Generic Programming

查看:91
本文介绍了继承与虚拟函数VS泛型编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要明白,是否真的继承&虚函数在C ++中不是必需的,并且可以使用通用编程实现一切。这来自 Alexander Stepanov ,并且我在观看的演讲是 Alexander Stepanov: STL及其设计原则

I need to Understand that whether really Inheritance & virtual functions not necessary in C++ and one can achieve everything using Generic programming. This came from Alexander Stepanov and Lecture I was watching is Alexander Stepanov: STL and Its Design Principles

推荐答案

我总是把模板和继承视为两个概念,在字面意义上:对我来说,继承是垂直的,从顶部的基类开始,并向下到越来越多的派生类。每个(公开的)派生类 基类的界面:狮子狗是一只狗是动物。

I always like to think of templates and inheritance as two orthogonal concepts, in the very literal sense: To me, inheritance goes "vertically", starting with a base class at the top and going "down" to more and more derived classes. Every (publically) derived class is a base class in terms of its interface: A poodle is a dog is an animal.

手,模板去水平:模板的每个实例具有相同的形式代码内容,但两个不同的实例是完全独立的,并没有看到对方。整数数组的排序在形式上与排序浮点数组相同,但整数数组与浮点数组不相关。

On the other hand, templates go "horizontal": Each instance of a template has the same formal code content, but two distinct instances are entirely separate, unrelated pieces that run in "parallel" and don't see each other. Sorting an array of integers is formally the same as sorting an array of floats, but an array of integers is not at all related to an array of floats.

由于这两个概念是完全正交的,它们的应用也是如此。当然,你可以设计你可以替换另一个的情况,但是当完成惯例,模板(通用)编程和继承(多态)编程是独立的技术,都有自己的位置。

Since these two concepts are entirely orthogonal, their application is, too. Sure, you can contrive situations in which you could replace one by another, but when done idiomatically, both template (generic) programming and inheritance (polymorphic) programming are independent techniques that both have their place.

继承是通过添加细节来使抽象概念变得更具体。通用编程本质上是代码生成

Inheritance is about making an abstract concept more and more concrete by adding details. Generic programming is essentially code generation.

作为我最喜欢的例子,让我提到两种技术如何在一个流行的实现中em> type erasure :单个处理程序类保存抽象容器类的私有多态指针,并且确定具体的派生容器类是模板化的类型推导构造函数。我们使用模板代码生成来创建任意派生类族:

As my favourite example, let me mention how the two technologies come together beautifully in a popular implementation of type erasure: A single handler class holds a private polymorphic pointer-to-base of an abstract container class, and the concrete, derived container class is determined a templated type-deducing constructor. We use template code generation to create an arbitrary family of derived classes:

// internal helper base
class TEBase { /* ... */ };

// internal helper derived TEMPLATE class (unbounded family!)
template <typename T> class TEImpl : public TEBase { /* ... */ }

// single public interface class
class TE
{
  TEBase * impl;
public:
  // "infinitely many" constructors:
  template <typename T> TE(const T & x) : impl(new TEImpl<T>(x)) { }
  // ...
};

这篇关于继承与虚拟函数VS泛型编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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