Babri
Babri

Reputation: 949

Overloaded operator for C++ class doesn't get called

In my class, I write the prototype (public) as:

bool operator< (const MyClass& obj);

I implement the method outside the class (in the same file) as:

bool MyClass::operator< (const MyClass& obj)
{
    cout << "operator< used" << endl;
    //do my work
}

The problem is that although my overloading operator gets called if I call it explicitly (like obj1->operator<(*obj2)) but not when called implicitly (like obj1 < obj2).

I have followed overloading tutorial from this article and I can't see what I'm missing or doing wrong.

Upvotes: 2

Views: 127

Answers (2)

Ran Regev
Ran Regev

Reputation: 361

obj1 and obj2 are pointers to MyClass. if you want to call operator < use: *obj1 < *obj2

Upvotes: 1

David G
David G

Reputation: 96800

obj1 and obj2 are pointers, so you do *obj1 < *obj2.

Upvotes: 6

Related Questions