Is it possible to define an overloaded operator[]
that takes more than one argument? That is, can I define operator[]
as follows:
//In some class
double operator[](const int a, const int b){
return big_array[a+offset*b];}
and later use it like this?
double b=some_obj[7,3];
No you can't overload operator[]
to take more than one argument. Instead of
double b = some_obj[7,3];
use
double b = some_obj[7][3];
This answer explains how to create a proxy object to be able to the latter.
Otherwise, you could just overload operator()
to take 2 arguments.
No. The idiomatic way to do that in C++ is
double b = some_obj[7][3];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With