@rkenmi - A guide to const in C++

A guide to const in C++


The Many Uses of const with Operator Overloading

A guide to const in C++


Back to Top

Updated on June 10, 2017

The Many Uses of const with Operator Overloading

const can be used in different ways depending on how an operator is overloaded. There are two major
cases to consider:

  1. If an operator is overloaded as a nonmember function (not part of the class definition)
  2. If an operator is overloaded as a member function (part of the class definition)

Case 1: Overloaded as a Nonmember Function

Here is an example of the + operator overloaded as a nonmember function:

const Weight operator +(const Weight& w1, const Weight& w2);

Notice we have three positions where we can place const and each one means something different.
The explanations are color-coded for clarity.

const: When you place const in the red position you are specifying that the object the function is
returning should be constant. Meaning, the anonymous object that gets returned should not be able to
be directly altered. This is normally the behavior we want, because when we overload an operator we
normally want to overload the operator in such a way that it retains some of the meaning of its nonoverloaded
form. In other words, you normally wouldn't want to do something like this:

(w1 + w2).someFunction()

because if you use the + operator in its normal way you wouldn't (and couldn't) do something like:

(2 + 2).someFunction()

So we make the returned anonymous object constant so that way we can preserve some of the original
functionality of how the + operator is used.
It is far more intuitive to assign the anonymous object that gets returned, to another variable of the
same type and THEN make function calls using the new variable such as in:

Weight w3 = w1 + w2;
w3.someFunction();

Now remember with this example, when you say w3 = w1 + w2 you are copying the member
variables of the anonymous object into the member variables of the w3 object. When you do, this w3 is
a brand new object in memory which is NOT constant so you can alter it any way you want.

const: When you place const in the blue position you are simply saying that the first object passed
into the function is not allowed to be altered by that function.

const: When you place const in the green position you are simply saying that the first object
passed into the function is not allowed to be altered by that function.

Case 2: Overloaded as a Member Function

Now lets look at an example of overloading + as a member function:

const Weight operator +(const Weight& w) const;

const: When you place const in the red position in this example, you are still specifying that the
object the function is returning should be constant. The rest of the explanation is the same as above.

const: When you place const in the blue position you are simply saying that the calling object
(which is implicitely passed into the function) not allowed to be altered by that function.

const: When you place const in the green position you are simply saying that the parameter object
(which is explictely passed into the function) is not allowed to be altered by that function.

Source: CS332C by Keenan Knaur


Article Tags:
C++constsyntax