Home > OOP > Subscript Operator Overloading

Why is Subscript operator used?

We use Subscript operator [ ] to access elements of arrays.

Why overload subscript operator?

The most common use for subscript operator overloading is when we check index out of bound for an array. It can be overloaded with the same syntax as discussed in operator overloading.

returnType operator [](int index)
{
     // body of overloaded operator.
}

returnType is same as for the array whose value we are going to access using subscript operator.
operator reserved keyword.
[ ] is the subscript operator.
It always takes an argument i.e. index which we are going to access.
Example program: –

#include "stdafx.h"
#include"iostream"

using namespace std;
class exStr
{
     char str[100];
     public:
          char  operator [](int i)
          {
               if (i < 0 || i > 100)
               {
                    cout << "index out of bound";
                    return NULL;
               }
               return str[i];
          }
          void input()
          {
               cout << "Please enter input string : ";
               cin >> str;
          }
          void display()
          {
               cout << endl << str;
          }
};

int main()
{
     exStr str1;
     str1.input();
     cout << "Value at 2nd place in array  = " << str1[1]<<endl;
     return 0;
}

Output: –
Subscript Operator Overloading

So far so good, when we overload it like this we can access the value of an array. But what if we want to store a value in a particular index or position in the array. Can we do with this overloaded definition?
No, we cannot assign the value to an array using this definition. What should we do know?
To solve this problem, we need to change our overloaded definition.
Here is how:

returnType & operator [](int index)
{
     // body of overloaded operator.
}

Here we added & operator i.e. we will return address of the index. In this way, we can assign a value as well.
Example program: –

#include "stdafx.h"
#include"iostream"

using namespace std;
class exStr
{
     char str[100];
     
     public:
          char & operator [](int i)
          {
               if (i < 0 || i > 100)
               {
                    cout << "index out of bound";
               }
               else
               {
                    return str[i];
               }
          }
          void input()
          {
               cout << "Please enter input string : ";
               cin >> str;
          }
          void display()
          {
               cout << endl << "String after Assigning value at an index = " <<str<<endl;
          }
};

int main()
{
     exStr str1;
     str1.input();
     cout << "Value at 2nd place in array  = " << str1[1];
     str1[1] = 'a';
     str1.display();
     return 0;
}

Output: –
Subscript Operator Overloading

In this way, we can both access and assign a value at a particular index of an array.
Note: – It is not necessary that argument to this overloaded function is always int, i.e. it can be a string value in case of a map (will be discussed later).

Leave a Reply

Your email address will not be published. Required fields are marked *