I have a templated class, and a subclass of the templated class in order to implement a function differently. Since I want to keep the flexibility of having multiple subclasses, I don't directly store the subclass itself. In Java, something like what I'm trying to do would work, but apparently it doesn't in C++.
template<class Res> class Loader {
public:
Res* loadFromFile(std::string filePath);
};
template<class Res> inline Res* Loader<Res>::loadFromFile(std::string filePath){return NULL;}
Subclass:
class TextureLoader : public Loader<sf::Texture> {
public:
sf::Texture* loadFromFile(std::string path);
};
inline sf::Texture* TextureLoader::loadFromFile(std::string path){
sf::Texture* texture = new sf::Texture();
texture->loadFromFile(path);
return texture;
}
Somewhere in another class I store an instance of a Loader:
Loader<Res> * loader;
My problem is, that even if I assign a TextureLoader instance to that loader variable, only the superclass-function is called, but not the actually implemented subclass function. How can I achieve to call the subclas function while keeping the flexibility of having multiple loaders?
loadFromFile
needs to bevirtual
– Innocent Bystander Jan 1 '15 at 15:19