【发布时间】:2019-12-26 23:32:10
【问题描述】:
我对重载运算符 += 进行了赋值,因此它将新数据添加到列表中。 List += data 因此应该意味着 list=list+data,这在逻辑上类似于 operator += 对 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不好:)
【问题讨论】:
-
通常你应该在类名中包含一个类模板的参数列表。这样一来,代码将更加自文档化,您不必给我们评论。
标签: c++ linked-list overloading add operator-keyword