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

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

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
// refernce 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]<<" ";

// 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

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

GATE CS Notes (According to Official GATE 2017 Syllabus)

GATE CS Corner

See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.
Category: C++