J
Jim Langston
Daniel Kraft said: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().