Search

9.8 — Overloading the subscript operator

When working with arrays, we typically use the subscript operator ([]) to index specific elements of an array:

However, consider the following IntList class, which has a member variable that is an array:

Because the m_list member variable is private, we can not access it directly from variable list. This means we have no way to directly get or set values in the m_list array. So how do we get or put elements into our list?

Without operator overloading, the typical method would be to create access functions:

While this works, it’s not particularly user friendly. Consider the following example:

Are we setting element 2 to the value 3, or element 3 to the value 2? Without seeing the definition of setItem(), it’s simply not clear.

You could also just return the entire list and use operator[] to access the element:

While this also works, it’s syntactically odd:

Overloading operator[]

However, a better solution in this case is to overload the subscript operator ([]) to allow access to the elements of m_list. The subscript operator is one of the operators that must be overloaded as a member function. An overloaded operator[] function will always take one parameter: the subscript that the user places between the hard braces. In our IntList case, we expect the user to pass in an integer index, and we’ll return an integer value back as a result.

Now, whenever we use the subscript operator ([]) on an object of our class, the compiler will return the corresponding element from the m_list member variable! This allows us to both get and set values of m_list directly:

This is both easy syntactically and from a comprehension standpoint. When list[2] evaluates, the compiler first checks to see if there’s an overloaded operator[] function. If so, it passes the value inside the hard braces (in this case, 2) as an argument to the function.

Note that although you can provide a default value for the function parameter, actually using operator[] without a subscript inside is not considered a valid syntax, so there’s no point.

Why operator[] returns a reference

Let’s take a closer look at how list[2] = 3 evaluates. Because the subscript operator has a higher precedence than the assignment operator, list[2] evaluates first. list[2] calls operator[], which we’ve defined to return a reference to list.m_list[2]. Because operator[] is returning a reference, it returns the actual list.m_list[2] array element. Our partially evaluated expression becomes list.m_list[2] = 3, which is a straightforward integer assignment.

In the lesson a first look at variables, you learned that any value on the left hand side of an assignment statement must be an l-value (which is a variable that has an actual memory address). Because the result of operator[] can be used on the left hand side of an assignment (e.g. list[2] = 3), the return value of operator[] must be an l-value. As it turns out, references are always l-values, because you can only take a reference of variables that have memory addresses. So by returning a reference, the compiler is satisfied that we are returning an l-value.

Consider what would happen if operator[] returned an integer by value instead of by reference. list[2] would call operator[], which would return the value of list.m_list[2]. For example, if m_list[2] had the value of 6, operator[] would return the value 6. list[2] = 3 would partially evaluate to 6 = 3, which makes no sense! If you try to do this, the C++ compiler will complain:

C:VCProjectsTest.cpp(386) : error C2106: '=' : left operand must be l-value

Dealing with const objects

In the above IntList example, operator[] is non-const, and we can use it as an l-value to change the state of non-const objects. However, what if our IntList object was const? In this case, we wouldn’t be able to call the non-const version of operator[] because that would allow us to potentially change the state of a const object.

The good news is that we can define a non-const and a const version of operator[] separately. The non-const version will be used with non-const objects, and the const version with const-objects.

If we comment out the line clist[2] = 3, the above program compiles and executes as expected.

Error checking

One other advantage of overloading the subscript operator is that we can make it safer than accessing arrays directly. Normally, when accessing arrays, the subscript operator does not check whether the index is valid. For example, the compiler will not complain about the following code:

However, if we know the size of our array, we can make our overloaded subscript operator check to ensure the index is within bounds:

In the above example, we have used the assert() function (included in the cassert header) to make sure our index is valid. If the expression inside the assert evaluates to false (which means the user passed in an invalid index), the program will terminate with an error message, which is much better than the alternative (corrupting memory). This is probably the most common method of doing error checking of this sort.

Pointers to objects and overloaded operator[] don’t mix

If you try to call operator[] on a pointer to an object, C++ will assume you’re trying to index an array of objects of that type.

Consider the following example:

Because we can’t assign an integer to an IntList, this won’t compile. However, if assigning an integer was valid, this would compile and run, with undefined results.

Rule: Make sure you’re not trying to call an overloaded operator[] on a pointer to an object.

The proper syntax would be to dereference the pointer first (making sure to use parenthesis since operator[] has higher precedence than operator*), then call operator[]:

This is ugly and error prone. Better yet, don’t set pointers to your objects if you don’t have to.

The function parameter does not need to be an integer

As mentioned above, C++ passes what the user types between the hard braces as an argument to the overloaded function. In most cases, this will be an integer value. However, this is not required -- and in fact, you can define that your overloaded operator[] take a value of any type you desire. You could define your overloaded operator[] to take a double, a std::string, or whatever else you like.

As a ridiculous example, just so you can see that it works:

As you would expect, this prints:

Hello, world!

Overloading operator[] to take a std::string parameter can be useful when writing certain kinds of classes, such as those that use words as indices.

Conclusion

The subscript operator is typically overloaded to provide direct access to individual elements from an array (or other similar structure) contained within a class. Because strings are often implemented as arrays of characters, operator[] is often implemented in string classes to allow the user to access a single character of the string.

Quiz time

1) A map is a class that stores elements as a key-value pair. The key must be unique, and is used to access the associated pair. In this quiz, we’re going to write an application that lets us assign grades to students by name, using a simple map class. The student’s name will be the key, and the grade (as a char) will be the value.

1a) First, write a struct named StudentGrade that contains the student’s name (as a std::string) and grade (as a char).

Show Solution

1b) Add a class named GradeMap that contains a std::vector of StudentGrade named m_map. Add a default constructor that does nothing.

Show Solution

1c) Write an overloaded operator[] for this class. This function should take a std::string parameter, and return a reference to a char. In the body of the function, first iterate through the vector to see if the student’s name already exists (you can use a for-each loop for this). If the student exists, return a reference to the grade and you’re done. Otherwise, use the std::vector::push_back() function to add a StudentGrade for this new student. When you do this, std::vector will add a copy of your StudentGrade to itself (resizing if needed). Finally, we need to return a reference to the grade for the student we just added to the std::vector. We can access the student we just added using the std::vector::back() function.

The following program should run:

Show Solution

2) Extra credit #1: The GradeMap class and sample program we wrote is inefficient for many reasons. Describe one way that the GradeMap class could be improved.

Show Solution

3) Extra credit #2: Why doesn’t this program work as expected?

Show Solution

9.9 -- Overloading the parenthesis operator
Index
9.7 -- Overloading the increment and decrement operators

185 comments to 9.8 — Overloading the subscript operator

  • vishs

    Hi,

    While I agree that overloading [] operator is helpful for better comprehension and easier looking syntax in the examples you have taken above

    But for other examples like:

    Suppose I do this:

    The above code is really not good, because class itself is having so many arrays and also non-array elements as members, in this case overloading as object[] doesn't give any idea of which list(list1 or list2) is being accessed and also what it means for "someotherData" member

    So in reality, most classes will be having many data members of different types, so overloading operators with class objects gives no idea of what members are getting affected and in which way, so i feel overloading operators reduce readability of code? is my understanding correct?

    • nascardriver

      Hi again,

      operators, just like poorly named functions, can always be used in a way that makes them more confusing than helpful. You _can_ overload `operator[]` for `Database`, but as you said, it doesn't make a lot of sense. `operator[]` is used for map- and list-like objects.
      Operators reduce code quality when they're used poorly, but increase it when used correctly. That's not a property unique to operators, you'll find it everywhere.

  • Guybrush87

    Hello,

    First of all, thank you for this great courses !

    I have a silly question, why the code above don't compile with the error "error: invalid conversion from 'const int*' to 'int*' [-fpermissive]"

    I don't modify nothing, why can I make my class member getList constant ?

    Thank you for your help.

    Guybrush87

    • `getList` is `const`.

      this would be legal if your code compiled, but obviously it shouldn't, because `list` is `const`.

      Don't mark `getList` as `const` or return a `const int*`.

      • Guybrush87

        Hello nascardriver,

        I'm sorry, I don't understand your answer, here is my code :

        For this code the compiler says :
        error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
          int* getList() const { return m_list; }
                                        ^~~~~~
        In my code there is nothing const except for the function member getList.
        What I don't understand is that my member getList doesn't modify any data member, why isn't it possible to declare it as a const member ?

        Thank you very much for your help !

        Guybrush87

  • potterman28wxcv

    Hello !

    The 3) part scares me a lot about C++ ! Because from a glance, the code looks alright, and you just wouldn't see such a bug easily. Sorting out that bug requires having knowledge of how std::vector is working internally.. I guess std::vector is widely used, but if you use some library defined by another programmer and only have the API, there is no way you could easily trace back that bug.

    Is there any general rule to follow in C++ to avoid writing the code of 3) ? That code really sends me shills because any reference could be "eventually dangling" as soon as you initialize them! I can't even imagine the amount of bugs that must have been caused by this kind of scenario in large projects..

    • Alex

      As a general rule, we always have to be careful when holding references or pointers to data that lives in dynamic memory, because the destruction of that memory may not be predictable.

      In this case, the scariness is mainly due to the fact that we don't expect operator[] to invalidate pointers or references, but it might. This is a function of using a std::vector to implement the class.

      I can think of two possible solutions here:
      1) Rewrite the interface to the class to make operations that might invalidate data always needs to be explicitly called by the user (e.g. have an add() function). At least that way, control of the lifetime of the data is under the user's control.
      2) Better, use an implementation that doesn't invalidate the current set of elements when new ones are added. std::map doesn't invalidate references or pointers when operator[] is used with it (https://en.cppreference.com/w/cpp/container/map/operator_at), and so would be a better choice to implement this class with.

  • George

    How are the constant and non-constant versions unique? Same return type and same number and type of arguments...

    • One is marked as `const`, the other one isn't.
      The `const` version is used for `const` objects, and also for non-const objects if no non-const version exists. ie. the type of the object the function is called on determines which function gets called.

  • noobmaster

    I edited function char& GradeMap::operator[](const std::string &name) in solution for 1c

    my compiler throws me a warning 'GradeMap::operator[]': not all control paths return a value
    can someone explain why?

  • mmp52

    Hello!

    in the for - each loop, what is the difference between making the iterator a reference or not? Also, if we use it like that how does compiler understand we did not mean the address but the iterated element's reference?

    thanks for the amazing content !!

    • Iterating over copies is slow. Non-fundamental types should always be passed/returned/iterated over by reference.
      The first part of a for-each loop is a declaration, address-of doesn't make sense at that point, just like with regular variable declarations.

  • Hai Linh

    Quick question: Is there any reason to make index non-const? It would make sense to make index const, that way the function cannot change the value of index.

  • Vir1oN

    Could you refresh my memory, why do we use return by address instead of return by reference here?

  • Anthony

    Hi Alex,

    Given the following class definition:

    The following works:

    However, if I change

    to

    the line will compile, but

    won't.

    What is the difference between

    and

    ? And why doesn't the line that follows compile?

    Thanks!

    • is a function declaration. This is known as C++'s most vexing parse, because you might expect it to be a default initialization.
      Brace initializers solve this problem.

  • Anthony

    This is a very interesting lesson. I initially omitted to use a reference within the for-each statement of question 1c, leaving a dangling reference. Code::Blocks picked it up with a warning about a local variable being returned. I'm learning :)

  • Tyson

    Hi,

    When I try to compile my solution or Alex' solution to Question-1C, I get this error "error: 'GradeMap::m_map' should be initialized in the member initialization list [-Werror=effc++]"

    However, I was able to overcome it by adding empty curly brackets next to m_map, the private member, like this:

    My question is, is this the correct way of solving the problem?

    Thank you for the great tutorials.

    • Alex

      This issue has been fixed in the lesson. Thanks for pointing out the omission.

      • Hamza

        I don't know why would it be a problem to leave it uninitialized .. I compiled it without curly brackets and it worked fine!

        • Not explicitly initializing it will still initialize it to an empty vector.
          This only works, because `std::vector` a class-type and has automatic storage duration. If it was a fundamental type, you'd have to initialize it or risk undefined behavior on use. Since differentiating between the types (Initialize one but not the other) is extra work and could break if a type is changes in the future, initializing all types the same is best.

  • Louis Cloete

    @Alex, I don't know if you know Rust, but I have started learning it as well. In Rust, Quiz q3 will not compile, because the compiler enforces that you have only one "mutable borrow" (non-const reference in C++) OR any amount of "immutable borrows" (const references in C++). This means you can't mutate a vector while there are other references to it. This prevents dangling references/pointers and data races at compile time. Knowing this, I immediately spotted the error.

    I find that my dabbling with Rust causes me to think safer in C++, because I had to fix many compiler errors because of code like this, which is legal in C++, but causes compiler errors in Rust.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]