C++ Operator Overloading

JellyWorld

Newbie
Joined
Mar 22, 2005
Messages
2,852
Reaction score
1
Just a little question, how do you overload the multiple [] operator eg.

Object xxx;
int i = xxx[0][1];

I know for single [] it's

operator[](int index)

or something like that.
 
I would overload the () operator instead since it allows more than one parameter to be passed to it.

e.g.

Code:
int MyClass::operator() ( unsigned i , unsigned j )
{
    return MyClassArray[ i ][ j ];
}

If you have a two dimensional array of integer values. Obviously throw in some error checking.
 
The other way of doing it us to use what's called a proxy class to do it. Google for more info if you're interested, but overloading the () operator is much simpler.
 
Back
Top