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.

dark_shadow
  • 3,503
  • 11
  • 56
  • 81
  • Your overloaded operators need to return a value. See http://stackoverflow.com/questions/4421706/operator-overloading – Thomas Matthews Apr 20 '12 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. – Robᵩ Apr 20 '12 at 20:35

3 Answers3

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++;
}
Ben Cottrell
  • 5,741
  • 1
  • 27
  • 34
  • 3
    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: http://stackoverflow.com/questions/4421706/operator-overloading – Thomas Matthews Apr 20 '12 at 19:50
  • @ThomasMatthews good point, I agree it's important to preserve the semantics of the operators – Ben Cottrell Apr 20 '12 at 20:02
  • @ThomasMatthews: A great link.Thanks for it. – dark_shadow Apr 20 '12 at 20:09
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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
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.

chris
  • 60,560
  • 13
  • 143
  • 205