【发布时间】:2012-12-12 07:21:08
【问题描述】:
举个例子:
<type>& operator+(const <type>& rhs) {
// *this.data + rhs
}
如何在
如果我编码:
<type>& operator+(const <type>& rhs) {
<type> cell;
switch(rhs.type {
case DOUBLE:
{
cell.data = (double)cell.data + (double)rhs.data;
}
return cell;
}
我返回一个临时堆栈值,并收到一条错误消息。
如果我编码:
<type>& operator+(const <type>& rhs) {
*this.data = *this.field + rhs.data;
return *this;
}
我覆盖了这不是添加的意图。
这只是一个例子。 “真实”代码要求我能够添加(减去,...)任意数量的输入数据类型,这反过来又要求返回值能够适应多种类型中的任何一种,它可以并且确实如此.
【问题讨论】: