This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: ambiguous overload ?
- To: sidster <patrick at mail dot boxsoft dot com>
- Subject: Re: ambiguous overload ?
- From: Zhang Wei Feng <weifeng dot zhang at alcatel dot be>
- Date: Wed, 05 Jul 2000 12:27:04 +0200
- CC: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Organization: Alcatel Telecom
- References: <3962FD7D.8FADE7E4@alcatel.be> <20000705025155.D3936@3eye.boxsoft.com>
- Reply-To: weifeng dot zhang at alcatel dot be
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