【发布时间】:2013-03-11 07:18:23
【问题描述】:
我有这样的课:
class Node {
// data
Node* next;
void operator++(int);
};
当我像这样定义后增量运算符时:
void Node::operator++(int) {
this = this->next;
}
我在this = this->next; 上收到错误Expression is not assignable。
这是怎么回事?如何让this 指向next?
【问题讨论】:
-
this只能用作用于引用当前实例的指针。分配给this是没有意义的。你为什么要这样做? -
也许你可以换成
Node* Node::operator++(int) { return this->next; } -
顺便说一句,postfix
operator++返回 void 是相当奇怪的。这势必会让用户感到困惑。人们会期望它返回Node。 -
@Jichao
operator++(int)ofNode应该返回Node,而不是Node*。
标签: c++