Given the following example, why do I have to explicitly use the statement b->A::DoSomething() rather than just b->DoSomething()?
Shouldn’t the compiler’s overload resolution figure out which method I’m talking about?
I’m using Microsoft VS 2005. (Note: using virtual doesn’t help in this case.)
class A { public: int DoSomething() {return 0;}; }; class B : public A { public: int DoSomething(int x) {return 1;}; }; int main() { B* b = new B(); b->A::DoSomething(); //Why this? //b->DoSomething(); //Why not this? (Gives compiler error.) delete b; return 0; }
The two “overloads” aren’t in the same scope. By default, the compiler only considers the smallest possible name scope until it finds a name match. Argument matching is done afterwards. In your case this means that the compiler sees
B::DoSomething. It then tries to match the argument list, which fails.One solution would be to pull down the overload from
AintoB‘s scope: