Code:
std::map<int, int> mymap;
mymap[5] = 10; // What would happen here if mymap returns, say, char*?
In the code above, the compiler will assign 10 to whatever is returned from mymap[5].
So if mymap[5] returned char* instead of int&, what would happen? Compile error, and not something you'd expect!
And if it merely returned int, the you would assign to a temporary!
In other words, you don't make any change in the map at all.
Code:
class myClass
{
int data[];
int maxSize;
...
public:
...
int& operator[] (int index)
{
if (index < maxSize)
return data[index];
else
...
}
};
And please, write the class the right way