I'm trying to write a Testbench that can test different implementations of an Interface. Coming from Java I would love to just specify an Interface, create some Classes that implement it and write a class Testbench<T extends MyInterface>
Transforming this approach to C++ gives something like:
// The Interface
class Animal {
public:
Animal(int age) {};
virtual ~Animal() {};
virtual void Say();
};
// An Implementor
class Cow : Animal {
private:
int age;
public:
Cow(int age) : Animal(age) {
this.age = age;
};
void Say() {
std::cout << "I'm an " << age << " year old Cow" << std::endl;
}
}
Next I define the templated class that can test different Animals:
template<> void AnimalTestbench<class T> {
static void TestSay();
}
But when I try to implement the TestSay method, it gives me "Allocation of incomplete type T"
template<> void AnimalTestbench<class T>::TestSay() {
T *animal = new T(5);
animal->Say();
}
Of course I didn't specify that T should be an Animal, this is my first question. The latter is: why does this code fail?
I've heard that Templates are a fancy way for Macros that know about types, but if it's a Macro in a way, then the compiler should first replace T with my (complete) Type which he should be able to allocate and instantiate.