Hello,
Given a class with [] operators for both lvalue and rvalue:
Code:
class A
{
public:
double items[10];
double operator [] (unsigned int i)
{
cout << "rvalue operator" << endl;
return items[i];
}
double &operator [] (unsigned int i) const
{
cout << "lvalue operator" << endl;
return items[i];
}
};
and a function with a const A& as argument
Code:
void theFunc(const A &a)
{
}
The following lines have the expected behavior:
Code:
A a;
double v = a[0]; // calls the rvalue [] operator
a[1] = 10; // calls the lvalue [] operator
But I wouldn't have expected this one:
Code:
theFunc(a); // calls the rvalue operator ! Why ?
Obviously, if I cast the argument with something like this
Code:
theFunc(static_cast<const A&>(a));
, it works as expected, but I'd rather use a cleaner solution.
Why the rvalue isn't used in that case ?
Does anyone know a solution to this issue ?
Thanks.
Ed