我有一个接口(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/