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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With