Feb 14, 2008 at 2:56am UTC
Hi
I've created a class with a single vector as a private member, and overloaded operator[] to get/set the nth vector element at [n]... no problem.
I now have a need to add more vectors to the same class.
Is there an easy way to overload operator[] to get/set elements from/for the different vectors that are members of the same class?
like
reference operator[]1(int); // to access the first vector
reference operator[]2(int); // to access the second vector
etc.
Thanks
Richard
Feb 14, 2008 at 10:44am UTC
Since that operator only takes one parameter, the only way to overload it would be use different parameter types.
It looks that the best approach would be a regular member function with two parameters, though.
Feb 14, 2008 at 10:48pm UTC
Yes, it is possible, but it is usually not worth the trouble and/or a bad idea.
Instead of overloading [], overload ().
You can check out sections 13.10..12 of the C++ FAQ Lite for more info.
Hope this helps.