"Aff@n" <af********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
hi,
i wold like to ask, why is it when overloading operator[] we send int
value is there any way around?.
e.g.
class abc
{
protected:
int *a;
private:
int operator[](int );
here you are telling it to take an int value. You want it to take something
else, have it.
int operator[]( const std::string& );
int operator[]( float );
int operator[]( const MyClass& );
};
int abc::operator[](int x)
{
int z;
z=a[x];
return z;
};
Of course, you'd want to do somethign meaningful with the parameter. In one
of my classes I am passing a std::string& which I am using to look up the
value in a map with std::string as the key.
Proxy operator[]( const std::string& Key )
{
return Proxy(*this, Key);
}
Of coure I'm not returning an int either, but my own class. Why I'm doing
this really has nothing to do with your question though, this is just to
show you that operator[] can accept and return anything.