Possible Duplicate:
How to deduce the type of the functor’s return value?
Given the following examples:
<type>& operator+(const <type>& rhs) {
// *this.data + rhs
}
How do I return the value of the summation in an object of type < type>?
If I code:
<type>& operator+(const <type>& rhs) {
<type> cell;
switch(rhs.type {
case DOUBLE:
{
cell.data = (double)cell.data + (double)rhs.data;
}
return cell;
}
I return a temporary stack value, and receive an error message.
If I code:
<type>& operator+(const <type>& rhs) {
*this.data = *this.field + rhs.data;
return *this;
}
I overwrite this which is not the intent of the addition.
This is just an example. The 'real' code requires that I be able to add (subtract, ...) any number of input data types, which in it's turn requires that the return value is able to accommodate any on of several types, which it can and does.