I have an interface IOperand
:
class IOperand
{
public:
virtual IOperand * operator+(const IOperand &rhs) const = 0;
virtual std::string const & toString() const = 0;
}
And the class Operand
:
template <class T>
class Operand : public IOperand
{
public:
virtual IOperand * operator+(const IOperand &rhs) const;
virtual std::string const & toString() const;
T value;
}
The IOperand
class and the members functions operator+
and toString
prototype cannot be modified.
The member function operator+ has to add 2 values contained in 2 IOperand
. My issue is that this value can be an int, a char or a float but I don't know how to do that with templates. I have tried this :
template <typename T>
IOperand * Operand<T>::operator+(const IOperand &rhs) const
{
Operand<T> *op = new Operand<T>;
op->value = this->value + rhs.value;
return op;
}
my toString
method :
template <typename T>
std::string const & Operand<T>::toString() const
{
static std::string s; // Provisional, just to avoid a warning for the moment
std::ostringstream convert;
convert << this->value;
s = convert.str();
return s;
}
But the compiler does not find this->value
and rhs.value
because they're not in IOperand
.
EDIT : As advice in the comments, I added the toString
method in Operand
and Ioperand
, I don't really know if it could help.