如何重载 == 运算符?
How to overload == operator?
我正在使用 QT 设计应用程序,但我无法为 class 重载 ==。 Equals 工作得很好,但 == 不工作,我不明白为什么。
这是我的代码:
class connection{
public:
connection(State* s, Transition* t);
connection(Transition* t, State* s);
State* state;
Transition* transition;
bool equals (const connection* c) const; //edited, i missed const in copy paste
bool operator==(const connection &c) const;
};
bool connection::equals(const connection* c) const{
if (state->objectName() == c->state->objectName() && transition->objectName() == c->transition->objectName()){
return true;
}
return false;
}
bool connection::operator==(const connection &c) const{
if (this->state->objectName() == c.state->objectName() && this->transition->objectName() == c.transition->objectName()){
return true;
}
return false;
}
// and when i try to compare...
if (c->equals(d)) // works perfectly
if (c == d) // always return false
if(*c == *d) // works perfectly
取消引用运算符。
我正在使用 QT 设计应用程序,但我无法为 class 重载 ==。 Equals 工作得很好,但 == 不工作,我不明白为什么。
这是我的代码:
class connection{
public:
connection(State* s, Transition* t);
connection(Transition* t, State* s);
State* state;
Transition* transition;
bool equals (const connection* c) const; //edited, i missed const in copy paste
bool operator==(const connection &c) const;
};
bool connection::equals(const connection* c) const{
if (state->objectName() == c->state->objectName() && transition->objectName() == c->transition->objectName()){
return true;
}
return false;
}
bool connection::operator==(const connection &c) const{
if (this->state->objectName() == c.state->objectName() && this->transition->objectName() == c.transition->objectName()){
return true;
}
return false;
}
// and when i try to compare...
if (c->equals(d)) // works perfectly
if (c == d) // always return false
if(*c == *d) // works perfectly
取消引用运算符。