how to write operator overloading for operator [] in c++

This is the general signature of the operator[]:

T& operator[](any_type);

In your context it would look like this:

struct str {

    ...

    char& operator[](std::size_t pos) {
        return a[pos];
    }

};

Leave a Comment