I define two versions of overloaded operator[]
function in a class array
. ptr
is a pointer to first element of the array
object.
int& array::operator[] (int sub) {
return ptr[sub];
}
and
int array::operator[] (int sub) const {
return ptr[sub];
}
Now, if I define a const object integer1
the second function can only be called..... but if I make a non-const object and then invoke as below:
cout << "3rd value is" << integer1[2];
which function is called here?
See Question&Answers more detail:os