overload operator -

  • Thread starter Matthew Monopole
  • Start date
M

Matthew Monopole

Since in integer we can use either
a = -b
and
a = b - c

shouldn't it when we overload operator there'd be two way to overload the
minus operator?
if so, what's the prototypes?
class operator-()
and
class operator-(class)?
 
J

Jakob Bieling

Matthew Monopole said:
Since in integer we can use either
a = -b
and
a = b - c

shouldn't it when we overload operator there'd be two way to overload the
minus operator?

Yes, one is the unary version, the other is the binary version.
if so, what's the prototypes?
class operator-()
and
class operator-(class)?

Usually, you would pass const references to the operators and return
either a reference or a new object (depending on the operator). So for the
operator-, you would have the following:

myclass operator- ();
myclass operator- (myclass const& rhs);

This is pretty much what you said already. Now if you wanted to overload
operator-= as well, it would looke like this:

myclass& operator-= (myclass const& rhs);

Note that now you return a reference, because you do modify the current
object, instead of creating a new one.

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top