c++ - 为什么 C++ 容器不实现接口(interface)

标签 c++

<分区>

Possible Duplicate:
Why is the C++ STL is so heavily based on templates? (and not on interfaces)

为什么 STLQt 容器都不实现接口(interface)。例如,对于 vectorslists,它可以是 Enumerable

像这样:

template <typename T>
class Enumerable
{
public:
    virtual const T at(int k) = 0;
    //....
    virtual ~Enumerable() {}
};

template <typename T>
class Vector: public Enumerable<T>
{
public:
    virtual const T at(int k);
    //....
};

因此,我使用的代码迫使我使用其中使用的具体类型的容器。

最佳答案

您认为使用标准容器无法实现的目标是什么?你的问题的答案是他们不需要。使用模板,您可以以零运行时成本获得接口(interface)带来的所有优势。

关于c++ - 为什么 C++ 容器不实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285498/

相关文章:

c++ - 在 C++ 中生成正态分布的随机数

c++ - 应用程序中的自定义库,我是否必须两次包含库依赖 header ?

C++ 线程/vector 聊天

c++ - 如何通知系统创建新设备?

c++ - QScrollArea 内的 QGridLayout 内的方形自定义 QWidget

c++ - 如何在 CStdioFile 中写入空格?

c++ - 从数组(指针)中分配一个 vector

c++ - 将文件的内容添加到结构

c++ - 在 Cuda 中使用表达式模板构建 lambda 表达式

c++ - opencv,使用cmake构建c++项目时出现链接错误