【发布时间】:2010-09-09 11:27:39
【问题描述】:
鉴于以下示例,为什么我必须显式使用语句 b->A::DoSomething() 而不仅仅是 b->DoSomething()?
编译器的重载决议不应该弄清楚我在说哪种方法吗?
我正在使用 Microsoft VS 2005。(注意:在这种情况下使用虚拟没有帮助。)
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;
}
【问题讨论】:
-
我正在尝试使用指向 B 类的指针在 A 类中调用 DoSomething()。
标签: c++ function overloading resolution