Hello,
I have two operators, one returning a value
int operator()(int i) const
and second allowing to get the reference of a value (to write it):
int& operator()(int i)
And the problem is that no matter what always the second operator is called. Shouldn't it be called only when reference is needed?
I have written a simple class illustrating my problem:
#include <iostream>
class Test
{
private:
int tab[10],N;
public:
Test()
{
N=10;
for (int i=0;i<N;i++) tab[i]=i;
}
int operator()(int i) const
{
std::cout<<"why am i never called?"<<std::endl;
return tab[i];
}
int& operator()(int i)
{
std::cout<<"why am i always called?"<<std::endl;
return tab[i];
}
};
And the following code demonstrates the issue:
Test a;
int b=a(1); //uses the &operator
a(2); //uses the &operator
a(3)=8; //uses the &operator
I'll be grateful for any suggestions.