0

Is there any way to overload the > (greater than) operator, to be able to do something like:

myClass *a = new myClass(1);
myClass *b = new myClass(3);

if(a > b) //it should compare the int values from the constructors
  //do something

I've tried overloading the operator, but got various errors. I am using VC++.

7
  • It is not possible to overload operators where both of the arguments are primitive types. Pointers are primitive types. Commented Jul 2, 2012 at 23:00
  • Perhaps you mean: if (*a > *b) Commented Jul 2, 2012 at 23:01
  • If you use objects and not pointers. PS there is no need to use pointers in this example. Commented Jul 2, 2012 at 23:02
  • OK, then, no pointers, but still errors. It would be too much to paste the whole code in here... I can paste the error though. Commented Jul 2, 2012 at 23:04
  • 1
    I find this book good for how to do operator overloading, it covers each operator in turn and shows prescriptively how to do it. mindview.net/Books/TICPP/ThinkingInCPP2e.html Get the free e-book. Commented Jul 2, 2012 at 23:12

2 Answers 2

2

You cannot overload operators for pointers, because they are primitive types (and you cannot create an overload where both arguments are primitive types); instead, you can overload the operators for your objects of user-defined types (not pointer to them):

class myClass
{
    // ...
public:
    bool operator>(const myClass & right) const
    {
        return this->whatever()>right.whatever();
    }
};

myClass *a = new myClass(1);
myClass *b = new myClass(3);

if(*a > *b) //it should compare the int values from the constructors
  //do something

notice that, if you don't have a particular reason to allocate stuff on the heap, it's much better to just allocate it on the stack:

myClass a(1);
myClass b(3);
if(a > b)
    // do something
Sign up to request clarification or add additional context in comments.

3 Comments

OK, but I still get this error (Persoana is MyClass): error C2679: binary '>' : no operator found which takes a right-hand operand of type 'const Persoana' (or there is no acceptable conversion) 1> could be 'built-in C++ operator>(Persoana *, Persoana *)' 1> while trying to match the argument list '(Persoana *, const Persoana)'
@Eduard : That indicates you're comparing a Persoana to a Persoana* -- neither should be a pointer.
Got it to work. It was from a header file that I didn't write, it was using my class as a template, and had a pointer to it. Thanks a million!
2

Try

class MyClass
{
    int value;
    public:
        bool operator>(MyClass const& rhs) const
        {
             return value > rhs.value;
        }

        MyClass(int v):value(v){}

};

int main()
{
     MyClass a(1);
     MyClass b(3);

     if (a > b) { PLOP } else { POOP }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.