why subscript operator ([]) must be overloaded in member function?? one may say that because it will take one argument , but operation needs two operands...one the base address and second is index(offset).
cdnt it be , if cd have been treated like binary operator as well like this
class a
{
int arr[10];
friend int & operator(a &, int);
}

a .... where a is the first operand and i is the second....

so the friend function wd work like this
int & operator[](a& temp,int index)
{int a[] = a.arr;
return a[index];
}
now the problem is, y this implementation is not valid? y we have to do this in a member function only??

why not like this:

#include <iostream>
#include <string>

class A{
 std::string s;
 public:
  A(){ s = "1234567"; }
  char operator []( const int index ){ return s.at(index); }
};

int main (int argc, char const* argv[])
{
    A a;
    std::cout << a[0] << std::endl;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.