Hello again! In this video, we are going to look at which operators you should overload.
In principle, you can overload almost any operator that is provided by the language, but there are
some which are more useful than others.
The typical textbook or course or class example is to use the arithmetic operators, usually the plus
operator. Which is quite a good example, but it is only really useful for mathematical classes.
C++ now has complex and rational number classes.
So that is maybe not something you are going to need to use, to do very often. For general purpose programming,
the most useful operators are the assignment operator, which we have talked about before. The equality
and inequality operators, for testing whether two objects have the same value. The less than operator
for doing comparisons.
You can actually do any comparison using just one operator, and the less than operator is the one that is
used in C++.
And then finally, this rather mysterious one, the function call operator. Which we will talk about
later on in this section.
There are some operators which you should not overload or cannot overload. The logical "and" and "or" operators.
The built-in versions of these have special properties.
So if you have something like "a" and "b" and "c", they start executing from left to right.
So first it evaluates a. And they have a "short-circuit" evaluation.
So if, for example, a is true and b is false, then it will stop and return false.
It will not evaluate c.
You can overload these operators, but they will not have those properties.
So it is probably not something that is useful.
You can also overload the address-of operator and the comma operator, which is used for writing several
expressions in a single statement.
These are already defined for classes and if you redefine them, you are just going to confuse things.
And some that you are not allowed to overload: the scope operator - the double colon for namespaces or
class members - the dot operator for accessing struct and class members, the dot star operator for member
function pointers, and the ternary operator. And you are not allowed to overload those.
In general, you should think carefully before you add operators to a class. Only provides operators
which are actually needed or add value to the class.
So do not go mad and you write lots of overloads if you do not actually need them!
If you are writing a database connector class,
does it really need to be able to do arithmetic?
The return value from the operator should correspond to the equivalent in the language for built-in
or library class types.
If you have a logical or relational operator, it should return bool.
If it is an arithmetic operator, it should return the class type by value. And if it is an operator which
performs an assignment.
So something like plus equals, for example. It should return the object on the left of the operation
by reference, which is not a const reference.
And if you do all these, your operators will be consistent with the ones in the language and they will
be easier to use and less confusing.
OK, so that is it for this video.
I'll see you next time, but until then, keep coding!