Reputation: 71
I'm currently writing a double linked list using dynamic memory in C++ for one of my classes. I've already got the code written, I just had a question.
Our professor required us to write both an
int& operator[](int position)
AND an
int operator[](int position) const
function.
What was the point in doing two functions for the same operator? I'm sure there's some kind of logic behind it, haha. Is it just so I can do a = arr[i] and also do arr[i] = a?
Upvotes: 3
Views: 136
Reputation: 279255
Where you have a doubly-linked list object that is defined with the const
qualifier (commonly called "a const object") or referred to via a pointer-to-const or a reference-to-const, you want to be enable reading but not writing. The const
-qualified operator does that.
Where you have an object that is defined without the const
qualifier, or referred to via a pointer-to-non-const or a reference-to-non-const, you want to enable both reading and writing. The non-const
-qualified operator does that.
So, the reason you bother writing two overloads is to implement both behaviours.
If you only wanted reading, regardless of constness, then you would only write the const
version because const
-qualified member functions can be used to interact with both cases, whereas non-const
-qualified member functions can only be used on the names of non-const
-qualified objects (or via a pointer-to-non-const or a reference-to-non-const).
If you only wanted operator[]
to apply to non-const
objects then you would only write the non-const
-qualified version. This might seem strange, since you don't expect operator[]
to need the object to be modifiable, but as it happens this is exactly what std::map
does with operator[]
.
Upvotes: 0
Reputation: 53155
1) The const
variant is for reading, e.g. when you assign a value to a variable. The compiler will give you an error when trying to modify the list value at the specified index. Example for const is a = arr[i]
as you wrote.
2) The non-const variant
is usually used when you actually set the value at the certain index of the list. Although you could use this variant for getting a value as well. However, then the compiler would not raise an error when your intention is to just read and your code would accidentally write. That could lead to subtle bugs which you can avoid by using const
for such cases. Example for non-const is arr[i] = a
as you wrote.
If you have both versions present, the non-const version version will be called for reading when you execute the indexing on a non-const object.
Upvotes: 0
Reputation: 86
Its just if you have a const variable of your object, lets call it A const a;
then you are only allowed to call const functions, like your const overload of operator [] (...) const
, which only provides reading. You are not allowed to call the non const operator [] (...)
in this case.
Upvotes: 0
Reputation: 672
When your object is const, you can only use the const
version of the operator, which by virtue of being const doesn't allow modification of your object or returning non-cons pointers/references to internal members. So when your object is non-const you need a non-const operator that allows modifications so you can write foo[i]=n
Upvotes: 1
Reputation: 385154
Is it just so I can do
a = arr[i]
and also doarr[i] = a
?
Yes, that's what it's for.
const
or a non-const
list then you can perform the a = arr[i]
assignment;int&
is so that you can perform arr[i] = a
, which of course requires a non-const
list.Upvotes: 0
Reputation: 76305
If you have a const
list object, you should only be able to read values from it. If you have a modifiable one, you should be able to both read and write values. The first is done with the const
version, which will be called for a const
object.
Upvotes: 4