Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded "==" operator in C++ is not called?

I have a Task class wich has a string text private member. I access the variable trough const string getText() const;.

I want to overload the == operator to check if differents instances of the object have the same text.

I've declared a public bool operator==( const Task text2 ) const; on the class header and code it like this:

bool Task::operator==( const Task text2 ) const {
     return strcmp( text.c_str(), text2.getText().c_str() ) == 0;
}

But it was always returning false even when the strings where equal.

So I added a cout call within the bool operator==( const Task text2 ) const; to check if it was being called, but got nothing.

It seems that my custom == operator is never being called.

My header:

#ifndef TASK_H
#define TASK_H

#include <iostream>

using namespace std;

    class Task {
        public:
            enum Status { COMPLETED, PENDIENT };
            Task(string text);
            ~Task();
            // SETTERS
            void setText(string text);
            void setStatus(Status status);
        // GETTERS
            const string getText() const;
            const bool getStatus() const;
            const int getID() const;
            const int getCount() const;
            // UTILS
            //serialize
            const void printFormatted() const;
            // OVERLOAD
            // = expression comparing text
            bool operator==( const Task &text2 ) const;
        private:
            void setID();
            static int count;
            int id;
            string text;
            Status status;
    };

    #endif

Edited the overload operation to use a reference, and got away from strcmp:

bool Task::operator==( const Task &text2 ) const {
    return this->text == text2.getText();
}

Main file:

using namespace std;

int main() {
    Task *t = new Task("Second task");
    Task *t2 = new Task("Second task");

    cout << "Total: " << t->getCount() << endl;
    t->printFormatted();
    t2->printFormatted();

    if( t == t2 ) {
        cout << "EQUAL" << endl;
    }
    else {
        cout << "DIFF" << endl;
    }

    return 0;
}
like image 434
jviotti Avatar asked Nov 30 '22 02:11

jviotti


2 Answers

Task *t = new Task("Second task");
Task *t2 = new Task("Second task");
// ...
if( t == t2 ) {

You are not comparing Task objects, but pointers to Task objects. Pointer comparison is native to the language and compares identity of the objects (i.e. will yield true only if the two pointers refer to the same object or both are null).

If you want to compare the objects you need to dereference the pointers:

if( *t == *t2 ) {
like image 78
David Rodríguez - dribeas Avatar answered Dec 22 '22 01:12

David Rodríguez - dribeas


You wrote:

as a beginner in C/C++ I'm getting confused sometimes with pointers and references.

The solution to that problem is simple: don't use pointers. Unlike C, C++ allows you to write completely useful programs without directly using pointers.

Here is how you could have written your program:

int main() {
    Task t("Second task");
    Task t2("Second task");

    std::cout << "Total: " << t.getCount() << "\n";
    t.printFormatted();
    t2.printFormatted();

    if( t == t2 ) {
        std::cout << "EQUAL\n";
    }
    else {
        std::cout << "DIFF\n";
    }

    return 0;
}
  1. Don't call new. You really didn't need it. As the currently-accepted answer points out, the use of pointers is the root cause of your problem.

  2. Don't use using namespace std;. It introduces subtle bugs (none in your program, but it's best to avoid it.)

  3. Don't use std::endl if you mean '\n'. '\n' means "End this line." std::endl means "End this line and flush the output."

like image 37
Robᵩ Avatar answered Dec 22 '22 01:12

Robᵩ