Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 16th, 2005, 01:35 PM
jacob navia
Guest
 
Posts: n/a
Default Operator overloading advise

Hi
I am implementing operator overloading in the context of
the C language.

I would like to have your opinion about this:

Would it be a good idea to assume that the
operator != could be synthethized from

! a == b (operator_equal)

operator >= could be built from ! operator<
operator < could be built from ! operator>=

etc etc.

This would greatly reduce the number of operator
functions that the user needs to implement...

In the same style a == b implies that b == a isn't it?
This means that

int operator==(foo &a,int &b) is the same as
int operator==(int &b,foo &a)

making == and != commutative.

Another step would be to assume commutativity
in the arithmetic operators, what is kind of riskier.

A+B != B+A if you overload the "+" for strings:

"1234" + "5678" --> "12345678", but
"5678" + "1234" --> "56781234"

What guidelines should be sensible to follow?

Thanks in advance for your comments

jacob
  #2  
Old August 16th, 2005, 01:45 PM
Kyle
Guest
 
Posts: n/a
Default Re: Operator overloading advise

> What guidelines should be sensible to follow?

That heavily depends on the thing ur trying to model (although i cant
imagine right now types where a==b doesnt imply ! a!=b [im not that
imaginative though]).

As for the guidelines ... u should stay as close to the model ur
implementing as its possible. ppl using ur class would expect it to
behave acording to their common sense (misleading sometimes though), so
ill advice you to comment any non obvious operators acordingly.

[color=blue]
> Thanks in advance for your comments
>
> jacob[/color]
  #3  
Old August 16th, 2005, 03:05 PM
Josh Mcfarlane
Guest
 
Posts: n/a
Default Re: Operator overloading advise

jacob navia wrote:[color=blue]
> operator >= could be built from ! operator<
> operator < could be built from ! operator>=
> What guidelines should be sensible to follow?[/color]
For guidelines, check the chapter of the FAQ on Operator overloading.
http://www.parashift.com/c++-faq-lite

As for building operations off the inverse of the other operator, you
need to be really careful. Comparisons aren't always what you think.
See http://www.parashift.com/c++-faq-lit...html#faq-29.14 for an
example.

  #4  
Old August 16th, 2005, 06:35 PM
Greg
Guest
 
Posts: n/a
Default Re: Operator overloading advise


jacob navia wrote:[color=blue]
> Hi
> I am implementing operator overloading in the context of
> the C language.
>
> I would like to have your opinion about this:
>
> Would it be a good idea to assume that the
> operator != could be synthethized from
>
> ! a == b (operator_equal)
>
> operator >= could be built from ! operator<
> operator < could be built from ! operator>=
>
> etc etc.
>
> This would greatly reduce the number of operator
> functions that the user needs to implement...
>
> In the same style a == b implies that b == a isn't it?
> This means that
>
> int operator==(foo &a,int &b) is the same as
> int operator==(int &b,foo &a)
>
> making == and != commutative.
>
> Another step would be to assume commutativity
> in the arithmetic operators, what is kind of riskier.
>
> A+B != B+A if you overload the "+" for strings:
>
> "1234" + "5678" --> "12345678", but
> "5678" + "1234" --> "56781234"
>
> What guidelines should be sensible to follow?
>
> Thanks in advance for your comments
>
> jacob[/color]

Clearly behavior that conforms to the built in operators make the most
sense. C++ however lets the programmer define overloaded operators to
do anything, so it's up to the programmer to show some good sense.

Technically, all the equality and relational operators can be derived
from the less-than operator, <. So just implement operator<() and the
other operators' definitions will be boilerplate.

Greg

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles