By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

overload operator -

P: n/a
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)?
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Matthew Monopole" <ma*******@hotmail.com> wrote in message
news:ht*****************@newsread2.prod.itd.earthl ink.net...
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.