2

I was just reading about unary operator overloading using minus(-),increment(++).etc. I thought to write a code for the same just for practice.But when I tried to run it it give me error for both minus and increment operator.I think the problem is the way I'm calling the operator in main.Can anyone please what's the correct way of doing this ?

#include<iostream>

using namespace std;

class c{
   int x;
   int y;
   public:
      c(int a,int b){
             x=a;
             y=b;   
      }

      void operator -(){
             x=x+1;
             y=y+1;
      }

      void display(){
             cout<<x<<" "<<y<<"\n";
      }

};

int main()
{
     c obj(2,3);
     obj.display();
         obj- ;    //I think the error is on this line
     obj.display();
     return 0;
}

If I replace obj- with -obj the code works fine.Why is it so ? Same is the problem with ++ operator overloading(using ++obj works fine but obj++ doesn't work),why ?

Thanks.

2
  • Your overloaded operators need to return a value. See stackoverflow.com/questions/4421706/operator-overloading Commented Apr 20, 2012 at 19:51
  • @ThomasMatthews - to be clear, it is convention, and not the language, that requires overloaded operators to return a value. It is perfectly possible and acceptable for overloaded operators to return void. Doing so is legal, and precludes certain uses of the operator. Commented Apr 20, 2012 at 20:35

3 Answers 3

4

The unary minus operator - is a prefix operator only.

to overload suffix version of the ++ operator, you need a dummy int parameter. e.g.

struct foo
{
    void operator - ()
    {
        std::cout << "hello" << std::endl;
    }

    void operator ++ (int)
    {
        std::cout << "world" << std::endl;
    }
};

int main()
{
    foo bar;
    -bar;
    bar++;
}
Sign up to request clarification or add additional context in comments.

3 Comments

The overloaded increment operators return a value. See More Effective C++ by Scott Meyers, Item 6; Distinguish between prefix and postfix forms of increment and decrement operators." See also: stackoverflow.com/questions/4421706/operator-overloading
@ThomasMatthews good point, I agree it's important to preserve the semantics of the operators
@ThomasMatthews: A great link.Thanks for it.
2

First, you can't invent new operators, only redefine existing ones, so your unary post-minus can't be done. The post-decrement operator is (of course) two minus signs, not just one.

Secondly, when you define an increment or decrement operator, you give the function an (unused) int argument to distinguish the pre- form from the post-form; if the function has the argument, then it's a post-increment or post-decrement operation, but without it, it's pre-increment/pre-decrement.

Comments

1

The unary - operator is the negation operator. It's what happens when you say -5 or -var. You don't say 5- or var-. If you're after var - 3, overload the binary operator.

The postincrement operator has a dummy int argument to distinguish it from the preincrement operator.

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.