如何重载==运算符?

时间:2015-08-25 22:17:57

标签: c++ operators overloading

我使用QT设计应用程序而且我无法为一个课程重载==。 等于完美,但= =不起作用,我无法弄清楚原因。

以下是我的代码:

  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

1 个答案:

答案 0 :(得分:1)

if(*c == *d) //works perfectly

derefence运营商...... 我很抱歉这个愚蠢的问题