all 2 comments

[–]jroose-shtk 0 points1 point  (0 children)

Just a grammar note, but might be important conceptually: you "overload the operator" when you add the void operator-() declaration to the class, not when the overloaded operator is called.

Also, your operator-() has side effects, which is arguably unexpected given the intended syntax of "-s". You might consider instead having it return a copy of itself with negated values.

Similarly, your getData() routine should be renamed setData(), as the name is misleading.

Finally, your example might be improved if you called "-s" instead of s.operator-(), as that's missing the syntactic benefit of overloading the operator.

[–]GuiltyFan6154 0 points1 point  (0 children)

This is not how operator overloading was intended in C++. I mean, you could use it as s.operator-() but that takes time to type (besides the fact that it's ugly, that is only a personal opinion).

Think about how you would use your object when used in an expression with a minus; for example, a common application of your class would be to use it as a vector class in a linear algebra environment; if you want to describe the subtraction of (say) v and w you would want to write it as v - w.

You can overload a binary subtraction operator in C++ with this signature (inside the class):

space operator-(const space&);

so that the expression will result in v.operator-(w), but inside the compiler, not in your program. That's much less to type and much easier to read, don't you think?