10

Does anyone have a summary of boilerplate declarations for C++ operator overloading? A one page pdf would be nice. It would help us forgetful people having to stop and think about where to put our const and & and friend etc.

wxffles
  • 864
  • 6
  • 20
  • +1 it might also help self learners (like me) not going crazy (yep, just like me ;) ) – BlackBear Jan 23 '12 at 19:34
  • 7
    understanding is almost always better than copying. – AJG85 Jan 23 '12 at 19:35
  • 6
    Understanding and productivity aids aren't mutual exclusive. – Duck Jan 23 '12 at 19:39
  • You should just read this, and learn the ins and outs of them. http://www.parashift.com/c++-faq-lite/operator-overloading.html – Josh Jan 23 '12 at 19:43
  • 1
    @Duck The point was with understanding the reference is no longer needed. Cheatsheets and google are fine for rarely used algorithms but when and why to use keywords such as `const` is something that should simply be known. – AJG85 Jan 23 '12 at 19:50
  • 1
    @AJG85 Agree that understanding should be first. Why isn't reference needed? Unless you have photographic memory... – Tae-Sung Shin Jan 23 '12 at 19:55
  • @AJG85 - beyond the handful of common operators I would suggest most overloads do fall into the rarely used category for most people. I have set up a few templates for these in my editors. It doesn't relieve me of some copy & pasting nor thinking about the correctness of what I am inserting but it does eliminate some steps and starting from square one on things I may do once every six months. – Duck Jan 23 '12 at 19:59
  • @AJG85: I frequently use a list to make sure I didn't miss any. There's been several times I'm making a number-like class and forgot `|=` – Mooing Duck Jan 23 '12 at 20:07
  • 1
    See http://stackoverflow.com/questions/4421706/operator-overloading – Ben Voigt Jan 23 '12 at 20:45
  • I agree that's quite true for the less used operators. [This might be a useful reference](http://stackoverflow.com/questions/4421706/operator-overloading) – AJG85 Jan 23 '12 at 20:46

3 Answers3

5

Wikipedia has a pretty nice entry:

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Complete with prototypes for both member and global functions, where applicable.

pezcode
  • 5,490
  • 2
  • 24
  • 37
  • That almost exactly what I was looking for. Just need someone to condense it down to a handy document I can take on the road. Might make one of my minions do it. Any suggestions for a good place to host and share such a document? – wxffles Jan 23 '12 at 21:26
  • Ironically, [Gary Buyn](http://stackoverflow.com/users/749517/gary-buyn) had an almost identical answer 6 minutes earlier which he deleted. – Ben Voigt Jan 23 '12 at 21:38
1

Summary:

  • Assignment and compound assignment operators must be members, not friends.

  • Use the copy-and-swap idiom and pass-by-value for assignment operators. This gives you exception safety and handles the "assign-to-self" case.

  • Operators where the custom class can appear as either operand should be friends.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Not exactly what I was after, but you make some good points that should be included in a cheat sheet. – wxffles Jan 23 '12 at 21:27
0

The SPARKCHARTS C++ reference sheet has a nice mini-summary of operator overloading. They were kind enough to put it up on the Web:

C++ Operator Overloading

ahoffer
  • 6,347
  • 4
  • 39
  • 68
  • 3
    ...and they aren't using `const` right, so this really doesn't answer the OP's question. – Ben Voigt Jan 23 '12 at 19:40
  • Agreed. Const correctness is worth considering. How would you re-write the Sparkchart summary to include const correctness? (In fact, how would a C++ maven re-write the Sparkchart summary to be, in general, more useful?) – ahoffer Jan 23 '12 at 19:51