How to have a policy class implement a virtual function?
Posted
by dehmann
on Stack Overflow
See other posts from Stack Overflow
or by dehmann
Published on 2010-03-07T20:43:07Z
Indexed on
2010/03/08
0:12 UTC
Read the original article
Hit count: 362
I'm trying to design a policy-based class, where a certain interface is implemented by the policy itself, so the class derives from the policy, which itself is a template (I got this kind of thinking from Alexandrescu's book):
#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 {
};
Then, in the main()
function, I call test()
on (potentially) various different objects that all implement the same interface:
int main() {
std::vector<TestInterface*> foos;
foos.push_back(new Foo<TestImpl1>());
foos[0]->test();
delete foos[0];
return 0;
}
It doesn't compile, though, because
the following virtual functions are pure within ‘Foo<TestImpl1>’:
virtual void TestInterface::test()
I thought TestInterface::test()
is implemented because we derive from TestImpl1
?
© Stack Overflow or respective owner