通过模板 : why doesn't this work? 的 C++ 混合

标签 c++ mixins

我有一个接口(interface),它作为一个抽象基类实现,具有许多纯虚拟公共(public)方法。这些纯虚函数可以使用模板实现,因为子类之间的差异不大——所以我的想法是使用多重继承来混合提供实现的适当模板化的帮助类。但是,编译器提示基类是抽象的;它没有考虑 helper mix-in 的实现,因此认为没有所需方法的实现。

例如:

class TrivialList {
    int count;
public:
    TrivialList(int count) : count(count){}
    virtual double Average() const=0;
    int Count() const {return count;}
    virtual ~TrivialList(){}
};
template<typename TIndexable> class AverageHelper {
public:
    double Average() const {
        TIndexable const & self = static_cast<TIndexable const &>(*this);
        double sum=0.0;
        for(int i=0;i<self.Count();++) sum += self.Get(i);
        return sum / self.Count();
    }
};
class IndexableList : public TrivialList, public AverageHelper<IndexableList> {
    std::vector<double> backend;
public:
    IndexableList(int count) : TrivialList(count), backend(count) { }
    double & Get(int i) { return backend[i];}
    double const & Get(int i) const { return backend[i];}
};
IndexableList * MakeList() {return new IndexableList(5);} //error!
//    cannot instantiate abstract class

我正在使用 MSC 10.0 (Visual Studio 2010);使用 g++ 4.5 时代码失败并出现类似错误。

Get 或我项目中的真实世界等价物不能是虚拟的,因为它们是非常小的操作,需要内联以获得足够的性能(想想 put-pixel/get-pixel)- 所以我需要将通用算法模板化,而不是通过虚函数调用来通用。

最佳答案

要通过模板实现混合,您需要实现抽象函数的模板从抽象基类派生。

因此您可以通过以下方式更改代码来修复代码:

// ...
template<typename TIndexable> class AverageHelper : public TriviaList{

// ...
class IndexableList : public AverageHelper<IndexableList> {

一般来说,如果您想提供多个混合,您可以使用虚拟继承以避免基类的实例倍增,或者使用链式继承,如下例所示:

class Abstract {
public:
    virtual void foo() = 0;
    virtual void bar() = 0;
};

template<class Base>
class FooImpl : Base {
public:
    void foo() { /* default foo implementation */ }
};

template<class Base>
class BarImpl : Base {
public:
    void bar() { /* default bar implementation */ }
};

class Derived : public BarImpl<FooImpl<Abstract> > {
    // You have both foo() and bar() implementations available
};

关于通过模板 : why doesn't this work? 的 C++ 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3092475/

相关文章:

c++ - 如何从给定键的 interval_maps 中获取值?

c++ - 如何使用 qDebug 打印字符串文字和 QString?

c++ - void foo(int &x) -> ruby ?通过引用传递整数?

c++ - 如何在两个 mixin 模板之间实现多态行为?

type-conversion - 将LESS中的rgba颜色定义字符串转换为颜色实例

oop - Mixin 与继承

使用非类型模板参数的 C++ 优化

C++ 头文件和源目录

css - 为什么我的定位混合在与 Sass 一起工作时在 Stylus 中不起作用?

css - @include makeColumn() Bootstrap Mixin 不工作