简体   繁体   中英

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

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!

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM