Should I perceive it as C++ flaw or there is some good reason why it is like that:
g++ -o XXX XXX.cpp
XXX.cpp: In function 'int main(int, char**)':
XXX.cpp:26: error: request for member 'operator[]' is ambiguous
XXX.cpp:13: error: candidates are: int b2::operator[](double)
XXX.cpp:7: error: int b1::operator[](int)
Overloading only works within the same scope. You have two operator[] in two separate classes, and thus in two separate scopes. It's complaining about the name ambiguity because it must resolve scope before it can resolve overloading.
In other words, overloading cannot occur across multiple objects.
Not sure if this would work to resolve the issue, but try the following:
1 2 3 4 5 6 7
class C:
public b1,
public b2
{
using b1::operator[];
using b2::operator[];
};
That might bring both operators into the same scope and allow overloading to occur. I haven't tried it though.
Ambiguity checking occurs before type checking, so overloading is not possible across classes. I was surprised to see the results in MS Visual C++ 6.0. It actually returned 1! Looks like the user-friendly-compiler aims for giving as few errors as possible :)
I would like to see the base class as scope resolution operator in action here.