ambiguous overload ?
Zhang Wei Feng
weifeng.zhang@alcatel.be
Wed Jul 5 03:27:00 GMT 2000
Hi, patrick
Thanks for your inputs!
I have the following example working, though the difference is quite subtle!
#include <iostream>
using namespace std;
class Overload {
int values[100];
public:
// return coefficient reference for use as lvalue
int& operator [](const int n) {
return values[n];
};
// return coefficient for use as rvalue
int operator [](const int n) const {
// NOTE without const, the compiler gets ambiguous error!
return values[n];
};
};
int main() {
Overload over;
for(int i = 0; i < 10 ; i++)
over[i] = i;
for(int i = 0; i < 10; i++)
cout << "over[" << i << "] is " << over[i] << endl;
}
So as I noted above that if you forget the "const" for the rvalue operator,
then compiler still reports error. However, this do change the signature
of the function, but my question is
"Is that really difficult for normal functions( non-member functions) to differentiate
them by applying something like "const" " ?
Just for curiosity ;-)
weifeng
More information about the Gcc
mailing list