我对重载运算符+ =进行了赋值,因此它将新数据添加到列表中。因此,列表+ =数据应表示列表=列表+数据,从逻辑上讲,它类似于运算符+ =对int等基本类型所做的操作。我不知道在这种情况下该怎么办。
List& List::operator+=(const T& newData)
{
last_ = (!first_ ? first_ : last_->next_) = new Elem(newData);
++listSize_;
return *this;
};
where T is from template <typenameT> class List {...} "
这样可以,我可以使用函数中的代码吗?>
List& addToList (const T& newData) {same code snippet}
,它能给我预期的结果吗?我不这么认为,因为它从未在代码中使用运算符本身,这让我感到困惑。
很明显,我是编码的新手,对我的cpp不好:)
我对重载运算符+ =进行了赋值,因此它将新数据添加到列表中。因此,列表+ =数据应表示列表=列表+数据,在逻辑上类似于运算符+ =对基本类型所做的操作...
如果为您的课程定义operator+=
,请在您的课程实例上调用它: