Hello!
I have a template class which inherits from a non-template base class. It has functions the base class doesn't have (because the base class would need the template argument). But I don't know how to properly use polymorphism though a base class pointer in this case.
Example:
And the template class:
Now I want to acess a instance of templateClass though a BaseNonTemplate Class pointer (because it is stored in a vector). How can I do this? I didn't get it compiling.
I have a template class which inherits from a non-template base class. It has functions the base class doesn't have (because the base class would need the template argument). But I don't know how to properly use polymorphism though a base class pointer in this case.
Example:
Code:
#ifndef BASENONTEMPLATECLASS_H_INCLUDED
#define BASENONTEMPLATECLASS_H_INCLUDED
class BaseNonTemplateClass{
public:
virtual BaseNonTemplateClass();
virtual ~BaseNonTemplateClass();
};
#endif // BASENONTEMPLATECLASS_H_INCLUDED
Code:
#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
#include "BaseNonTemplateClass.h"
template<class T>
class templateClass : public BaseNonTemplateClass
{
public:
templateClass();
virtual ~templateClass();
T* getData();
protected:
T* data;
private:
};
template<class T>
templateClass<T>::templateClass(){
data = new T();
}
template<class T>
T* templateClass<T>::getData(){
return data;
}
#endif // TEMPLATECLASS_H
Comment