I have an Interface (there missing a lot of members, but mind the fact this interface is mandatory). I will need 5 classes inheriting from it, which will have a _value attribute. So, ins\ tead of implement 5 classes(for char, short, int, float, double), I thought about a template class :
class my_interface
{
public:
virtual [various_types] getValue() const = 0;
};
template<typename T>
class my_class : public my_interface
{
private:
T _value;
public:
my_class(T value) : _value(value) {} // initialize the attribute on construct
virtual T getValue() const { return _value; }
};
...so that something like that could work :
void my_function()
{
my_inteface* a = new my_class<char>(42);
my_interace* b = new my_class<short>(21);
int result;
result = a->getValue() + b->getValue();
}
But I don't see how I could do. It seems you can't make templates on interface pure virtual. To me, the only way that could work would be to make getValue() to always return a double, since it is the highest sized type I need. However, I don't like that solution.