Suppose I have the following code:
namespace x{
class X{
virtual void x(){}
}
}
namespace y{
class Y : public x::X{
void x(){}
}
}
int main(){
x::X* a = new y::Y();
a->x::X::x(); //Tries to access x::X::x non-virtually.
//Fails, since it treats the first x as the
//function name instead of the namespace
}
As you see, the last line of the main method tries to call the x method of class x::X statically (i.e. non-virtually). However, this fails, since the namespace has the same name as the method (both are named x). Therefore, the compiler treats the first x as method name and then complains that x::X makes no sence.
The problem might seem hypothetical on first sight, but it really appears in our code: The namespace cannot be renamed that easily because it is the namespace of a big project. The method name cannot be changed because it has to adhere to some other super class that is out of our scope (library class).
So, is there any chance to disambiguate this syntax somehow so that I am still able to call the method x::X::x non-virtually?
Right now, the only solution I see is typdefing x::X and then using the typedef. Is there any solution without typedef?
That is actually a quite clear description of the problem. The solution is removing the
x::part of the qualification. Lookup will start inYand will fail to findXin that context, then it will move up the hierarchy and findXas the injected name inside theXclass.