This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: ambiguous overload ?


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





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]