Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » overloading operators rules
icon5.gif  overloading operators rules [message #19431] Wed, 03 December 2008 13:31 Go to next message
amando1957 is currently offline  amando1957
Messages: 57
Registered: November 2007
Location: Wien/Vienna/Viden
Member
Hi to all.

There are rules for overloading operators in C++ I'm not sure i got them all. I do not ask for a syntax tutor, its meant so "what makes sense".

- keep track of semantics, so that the operator performs it is assumed to do: the op+ should add anything.
- avoid a given overload provided by the lang: "int % int" is here already.

Anyone can tell me some further rules recommended?
Thank you for any reply.

Martin
Re: overloading operators rules [message #19432 is a reply to message #19431] Wed, 03 December 2008 14:45 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13055
Registered: November 2005
Ultimate Member
amando1957 wrote on Wed, 03 December 2008 07:31

Hi to all.

There are rules for overloading operators in C++ I'm not sure i got them all. I do not ask for a syntax tutor, its meant so "what makes sense".

- keep track of semantics, so that the operator performs it is assumed to do: the op+ should add anything.



Or, at least, keep the new semantics consistent Smile

Mirek
Re: overloading operators rules [message #19438 is a reply to message #19432] Wed, 03 December 2008 23:05 Go to previous messageGo to next message
captainc is currently offline  captainc
Messages: 278
Registered: December 2006
Location: New Jersey, USA
Experienced Member
This is more of an 'agreement' that is kept for a project team. I think we should make a Topic++ doc page about it so that we keep all U++ code consistent. Some sort of developer guidelines for contributing would be good.
Re: overloading operators rules [message #21284 is a reply to message #19431] Mon, 11 May 2009 16:47 Go to previous message
piotr5 is currently offline  piotr5
Messages: 103
Registered: November 2005
Experienced Member
I think a third rule would be to watch out for operator-priority-list. it wouldn't make much sense to define an operator which in combination with other operators needs to be put into brackets all the time. for example in boolean logic "*" is used for and, "+" is used for or, even though the two could be used the other way around, simply because it is more convenient to have a tighter binding between 2 things which live in an and-condition. (for example (a&b)|(c&d) is more natural for us to understand than (a|b)&(c|d)...)

another interesting rule which stl seems to obey is that some operators must have symmetric parameters-types. some algoritms of stl are only defined when both parameters of the operator are of the same type. I don't remember where I had this problem though, but I think it had something to do with the "<" operator...
Previous Topic: I don't get some aspects of STL ... [pointless rant]
Next Topic: Basic library interface and separation problem
Goto Forum:
  


Current Time: Thu Jan 28 02:32:32 CET 2021

Total time taken to generate the page: 0.01588 seconds