WEBVTT

00:00.210 --> 00:04.410
Hello again! In this video, we are going to look at which operators you should overload.

00:06.080 --> 00:10.640
In principle, you can overload almost any operator that is provided by the language, but there are

00:10.640 --> 00:12.410
some which are more useful than others.

00:13.670 --> 00:20.360
The typical textbook or course or class example is to use the arithmetic operators, usually the plus

00:20.360 --> 00:25.310
operator. Which is quite a good example, but it is only really useful for mathematical classes.

00:26.330 --> 00:29.480
C++ now has complex and rational number classes.

00:29.510 --> 00:36.280
So that is maybe not something you are going to need to use, to do very often. For general purpose programming,

00:36.290 --> 00:42.230
the most useful operators are the assignment operator, which we have talked about before. The equality

00:42.230 --> 00:49.910
and inequality operators, for testing whether two objects have the same value. The less than operator

00:49.910 --> 00:51.050
for doing comparisons.

00:51.410 --> 00:56.960
You can actually do any comparison using just one operator, and the less than operator is the one that is

00:56.960 --> 00:58.100
used in C++.

00:59.000 --> 01:04.420
And then finally, this rather mysterious one, the function call operator. Which we will talk about

01:04.460 --> 01:05.570
later on in this section.

01:09.490 --> 01:17.140
There are some operators which you should not overload or cannot overload. The logical "and" and "or" operators.

01:17.500 --> 01:20.200
The built-in versions of these have special properties.

01:20.560 --> 01:26.410
So if you have something like "a" and "b" and "c", they start executing from left to right.

01:26.470 --> 01:30.850
So first it evaluates a. And they have a "short-circuit" evaluation.

01:30.850 --> 01:35.680
So if, for example, a is true and b is false, then it will stop and return false.

01:36.160 --> 01:37.270
It will not evaluate c.

01:37.270 --> 01:42.190
You can overload these operators, but they will not have those properties.

01:42.670 --> 01:45.100
So it is probably not something that is useful.

01:48.550 --> 01:55.840
You can also overload the address-of operator and the comma operator, which is used for writing several

01:55.840 --> 01:57.340
expressions in a single statement.

01:59.170 --> 02:03.580
These are already defined for classes and if you redefine them, you are just going to confuse things.

02:05.320 --> 02:12.700
And some that you are not allowed to overload: the scope operator - the double colon for namespaces or

02:13.030 --> 02:21.610
class members - the dot operator for accessing struct and class members, the dot star operator for member

02:21.620 --> 02:27.130
function pointers, and the ternary operator. And you are not allowed to overload those.

02:31.730 --> 02:37.130
In general, you should think carefully before you add operators to a class. Only provides operators

02:37.130 --> 02:39.490
which are actually needed or add value to the class.

02:39.500 --> 02:43.070
So do not go mad and you write lots of overloads if you do not actually need them!

02:43.850 --> 02:45.710
If you are writing a database connector class,

02:45.710 --> 02:47.840
does it really need to be able to do arithmetic?

02:49.400 --> 02:54.800
The return value from the operator should correspond to the equivalent in the language for built-in

02:54.800 --> 02:56.720
or library class types.

02:58.640 --> 03:02.600
If you have a logical or relational operator, it should return bool.

03:03.110 --> 03:09.380
If it is an arithmetic operator, it should return the class type by value. And if it is an operator which

03:09.380 --> 03:10.560
performs an assignment.

03:10.580 --> 03:17.270
So something like plus equals, for example. It should return the object on the left of the operation

03:17.690 --> 03:20.540
by reference, which is not a const reference.

03:21.890 --> 03:26.330
And if you do all these, your operators will be consistent with the ones in the language and they will

03:26.330 --> 03:28.340
be easier to use and less confusing.

03:29.360 --> 03:30.860
OK, so that is it for this video.

03:31.340 --> 03:34.310
I'll see you next time, but until then, keep coding!
