![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
Next: A class definition example
Up: More on Classes
Previous: Abstract Classes
If you need to redefine operators remember to redefine
then consistently (if you redefine '+' don't forget '+=') and
remember that you can't change precedence order.
The default assignment operator does a simple copy of members,
which might not be what you want.
Examples of redefining operators are in most books.
Some things to remember when redefining assignment operators are
- that reference parameters help to overload operators efficiently
(objects aren't copied) while keeping their use intuitive (pointers
would require the user to supply addresses of objects).
- to deal with situations where a variable is assigned to itself.
A pointer/reference to the object on the right
of the = will be given to the function explicitly. A pointer to
the object that the function is a member of is supplied implicitly
to all non-static member functions. It's called this, so
if this equals the supplied pointer, you'd usually want to do
nothing.
- to return a const. This catches errors like (i+j)=k
- to return *this so that assignments can be chained (i.e.
i=j=k becomes possible)
Next: A class definition example
Up: More on Classes
Previous: Abstract Classes
Tim Love
2001-07-05