473,394 Members | 1,866 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,394 software developers and data experts.

When to use overloaded operators

Hi all,

I'd like to know your opinion on when you think overloaded operators
should/could be used instead of "ordinary methods". Of course, they are
essential for generic programming and there are some nice "hacks" for
expression templates, like Boost's Spirit library.

But personally, I tend to use them also for "methods" of objects, which
to something "conceptually similar" to what the overloaded operator does
usually.

For instance, in a current project of mine, I have a class whose
instances define behaviour for "merging" with other objects of that
class and for "finding the differences"--this made me think to have
operator| instead of merge and operator& instead of comparation.

Would you recommend such practice or should I stick to "merge" and
"compare" as simple method names?

Yours,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Aug 5 '07 #1
4 1386
"Daniel Kraft" <d@domob.euwrote in message
news:f9**********@newsreader2.utanet.at...
Hi all,

I'd like to know your opinion on when you think overloaded operators
should/could be used instead of "ordinary methods". Of course, they are
essential for generic programming and there are some nice "hacks" for
expression templates, like Boost's Spirit library.

But personally, I tend to use them also for "methods" of objects, which to
something "conceptually similar" to what the overloaded operator does
usually.

For instance, in a current project of mine, I have a class whose instances
define behaviour for "merging" with other objects of that class and for
"finding the differences"--this made me think to have operator| instead of
merge and operator& instead of comparation.

Would you recommend such practice or should I stick to "merge" and
"compare" as simple method names?
Operator overloading only makes sense when the functionality you are
creating is related somewhat to the operator. operator| for a merge and
operator& for a comparison don't make much sense to me. It would take a
stretch of the imagination for a programmer to figure out that operator| is
merging as a bitwise or "merges". It's not intuitive. Personally, I would
go with merge() and compare().
Aug 5 '07 #2
On 2007-08-05 13:36, Daniel Kraft wrote:
Hi all,

I'd like to know your opinion on when you think overloaded operators
should/could be used instead of "ordinary methods". Of course, they are
essential for generic programming and there are some nice "hacks" for
expression templates, like Boost's Spirit library.

But personally, I tend to use them also for "methods" of objects, which
to something "conceptually similar" to what the overloaded operator does
usually.
Use operators where it would make sense to a person familiar with the
concepts modelled by the classes. Since most operators are mathematical
this rule kind of limits the applicability to mathematical concepts or
places where an specific meaning has been assigned to an operator
convention (i.e. using + to concatenate strings).
For instance, in a current project of mine, I have a class whose
instances define behaviour for "merging" with other objects of that
class and for "finding the differences"--this made me think to have
operator| instead of merge and operator& instead of comparation.

Would you recommend such practice or should I stick to "merge" and
"compare" as simple method names?
What is it you merge and compare? If they can be considered to be sets
then + would probably be better for union (merge) and - can be used for
set difference (probably not for intersection though).

--
Erik Wikström
Aug 5 '07 #3
On 2007-08-05 13:36, Daniel Kraft wrote:
>For instance, in a current project of mine, I have a class whose
instances define behaviour for "merging" with other objects of that
class and for "finding the differences"--this made me think to have
operator| instead of merge and operator& instead of comparation.

Would you recommend such practice or should I stick to "merge" and
"compare" as simple method names?
Erik Wikström <Er***********@telia.comwrites:
What is it you merge and compare? If they can be considered to be sets
then + would probably be better for union (merge) and - can be used
for set difference (probably not for intersection though).
I guess * may be used for intersection and ^ for symmetric difference
(ie. ((a\b) + (b\a))).

However, | and & may make some sens as well - ie. a | b -- elements in
set a OR set b, a & b -- elements in set a AND set b -- even though I'd
prefer +, -, *, ^.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Aug 5 '07 #4
On Aug 5, 8:36 pm, Daniel Kraft <d...@domob.euwrote:
For instance, in a current project of mine, I have a class whose
instances define behaviour for "merging" with other objects of that
class and for "finding the differences"--this made me think to have
operator| instead of merge and operator& instead of comparation.
I think you've answered your own question here: for int, the compiler-
generated operator| might reasonable be though of as merging the
values, but operator& does nothing like "finding the differences". It
actually finds the non-different bits, i.e. the common bits. So, if
you yourself picked a broken analogy, what chance have other people
got to use your class properly? Other people who've replied have
already suggested other operators.

So, in general it's best to stay away from the whole issue unless the
choice of operator is very clear-cut, and you find people's
expectation of the operators' affects are largely consistent (i.e.
everything intuitive). But, if the class will be deployed in a very
specific yet large area of code, then it may be worth using operators
that are initially confusing if they - once learned - provide
substantial benefits in concisions and clarity. Contrast this with
code to be spread thinly as occasional support code in other generally
unrelated code, where strange conventions won't be appreciated....

Cheers,

Tony

Aug 6 '07 #5

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

Similar topics

3
by: N4M | last post by:
Dear, I have problems with overloaded operators ++() and --(). MSVC++ 6.0 compiler gives errors, one is shown as below: " c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++' :...
20
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
10
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
4
by: Zytan | last post by:
http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/ Does anyone follow this? I know it's good to not have to deal with unsigned types in the interface, so that's great. But...
3
by: simbasaurus | last post by:
Hello! I am working on a really big project, in which the global new and delete operators can be overloaded, by including the file "overloadedops.h". The graphical interface was written using...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.