How to overload decrement(--) operator for functions which consist of other structure/classes

70 Views Asked by At

I found a lot information how to overload decrement(--) operators if your class has an int value. But I can not understand how to do the same with classes which contain structures which contain int values. I mean that:

class Vector{
   Point _end;
   Point _start;
   ...
 }

struct Point {
    int x;
    int y;
   ...
 }

And that is how my attempt to overload operator-- for Vector looks like:

Vector& Vector::operator--() {
    end.x--;
    end.y--;
    return *this;
}
Vector Vector::operator--(int){
    Vector temp = *this;
    *this--;
    return temp;
}

Also I got worning when I am using decrement to *this: Expression is not assignable So, how can I overload it in an appropriate way?

Thank you in advance!

1

There are 1 best solutions below

0
Vlad from Moscow On

This expression

*this--;

is equivalent to

* ( this-- );

because postfix operators have a higher precedence than unary operators.

So there is an attempt to decrease the pointer this.

What you need is

--*this;