I have the following data structures:
struct fastEngine { ... }
struct slowEngine { ... }
template<typename T>
class Car {
T engine;
vector<T> backupEngines;
virtual void drive() = 0;
}
class FastCar : public Car<fastEngine> {
virtual void drive() {
// use the values of "engine" in some way
}
}
class SlowCar : public Car<slowEngine> {
virtual void drive() {
// use the values of "engine" in some way
}
}
Car* getCarFromCarFactory() { // 1
if (randomNumber == 0)
return new FastCar();
else
return new SlowCar();
}
void main() {
Car* myCar = getCarFromCarFactory(); // 2
myCar->drive();
}
The compiler complains at locations 1 and 2 because it requires that I define Car* with template parameters. I don't care what templated version of Car I'm using, I just want a pointer to a Car that I can drive. The engine structs must be structs because they are from existing code and I don't have control over them.