473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Aug 16 '05 #1
3 1391
> 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.

Thanks in advance for your comments

jacob

Aug 16 '05 #2
jacob navia wrote:
operator >= could be built from ! operator<
operator < could be built from ! operator>=
What guidelines should be sensible to follow?

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.

Aug 16 '05 #3

jacob navia wrote:
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


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

Aug 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
12
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.