我正在尝试设计一个基于策略的类,其中某个接口由策略本身实现,因此该类派生自策略,该策略本身就是一个模板(我从 Alexandrescu 的书中得到了这种想法):
#include <iostream>
#include <vector>
class TestInterface {
public:
virtual void test() = 0;
};
class TestImpl1 {
public:
void test() {std::cerr << "Impl1" << std::endl;}
};
template<class TestPolicy>
class Foo : public TestInterface, TestPolicy {
};
然后,在main()
函数中,我test()
(可能)调用各种不同的对象,它们都实现了相同的接口:
int main() {
std::vector<TestInterface*> foos;
foos.push_back(new Foo<TestImpl1>());
foos[0]->test();
delete foos[0];
return 0;
}
但是,它不会编译,因为
the following virtual functions are pure within ‘Foo<TestImpl1>’:
virtual void TestInterface::test()
我认为TestInterface::test()
是因为我们派生自TestImpl1
?