Skip to content
Related Articles

Related Articles

Overloading Subscript or array index operator [] in C++

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 Jun, 2021

The Subscript or Array Index Operator is denoted by ‘[]’. This operator is generally used with arrays to retrieve and manipulate the array elements.
This is a binary or n-ary operator and is represented in two parts:

  1. postfix/primary expression
  2. expression

The postfix expression, also known as the primary expression, is a pointer value such as array or identifiers and the second expression is an integral value. In the second expression we can also include the enumerated values.



Example: Ramswarup[10];
Here the Ramswarup is an array and the above
statement print the value which is held by
Ramswarup at index position 10.

The primary-expression followed by the subscript operator is the pointer and it can be an integral value but the one must keep in mind that one of expression among two expressions must be a pointer value and it does not matter whether the second one is of an integral order or not.

// CPP program to demonstrate []
// operator
#include <iostream>
using namespace std;
int main()
    char name[] = "Ramswarup Tushar Nilesh Subhash";
    // Both of the statement prints same thing
    cout << name[5] << endl;
    cout << 5 [name] << endl;
    return 0;



In the above example both “cout” statement provides similar output due to the exclusive property of the subscript operator. The compiler reads both the statement in a similar way, so there is no difference between the *(name + 5) and the *(5 + name).

Positive and Negative subscripts
The first element of an array is stored at index 0. The range of a C++ array is from array[0] to array[size – 1]. However, C++ supports positive and negative subscripts. Negative subscripts must fall within array boundaries; if they do not, the results are unpredictable. The following code shows positive and negative array subscripts:

// CPP program illustrating the
// positive and negative subscripts
#include <iostream>
using namespace std;
// Driver Method
int main()
    int intArray[1024];
    for (int i = 0, j = 0; i < 1024; i++) {
        intArray[i] = j++;
    // 512
    cout << intArray[512] << endl;
    // 257
    cout << 257 [intArray] << endl;
    // pointer to the middle of the array
    int* midArray = &intArray[512];
    // 256
    cout << midArray[-256] << endl;
    // unpredictable, may crash
    cout << intArray[-256] << endl;



The negative subscript in the last line can produce a run-time error because it points to an address -256 positions which can be lower in memory than the origin of the array. The pointer midArray is initialized to the middle of intArray; it is therefore possible (but not recommended) to use both positive and negative array indices simultaneously. Array subscript errors do not generate compile-time errors, but they might yield unpredictable results.

We have introduced operator overloading. In this post overloading of index operator [] is discussed.
Following are some useful facts about overloading of [].
1) Overloading of [] may be useful when we want to check for index out of bound.
2) We must return by reference in function because an expression like “arr[i]” can be used an lvalue.

Following is C++ program to demonstrate overloading of array index operator [].

// Overloading operators for Array class
#include <cstdlib>
#include <iostream>
using namespace std;
// A class to represent an integer array
class Array {
    int* ptr;
    int size;
    Array(int*, int);
    // Overloading [] operator to access elements in array style
    int& operator[](int);
    // Utility function to print contents
    void print() const;
// Implementation of [] operator.  This function must return a
// reference as array element can be put on left side
int& Array::operator[](int index)
    if (index >= size) {
        cout << "Array index out of bound, exiting";
    return ptr[index];
// constructor for array class
Array::Array(int* p = NULL, int s = 0)
    size = s;
    ptr = NULL;
    if (s != 0) {
        ptr = new int[s];
        for (int i = 0; i < s; i++)
            ptr[i] = p[i];
void Array::print() const
    for (int i = 0; i < size; i++)
        cout << ptr[i] << " ";
    cout << endl;
// Driver program to test above methods
int main()
    int a[] = { 1, 2, 4, 5 };
    Array arr1(a, 4);
    arr1[2] = 6;
    arr1[8] = 6;
    return 0;


1 2 6 5 
Array index out of bound, exiting


1 2 6 5
Array index out of bound, exiting

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!