2009-07-16 137 views
13
class Base 
{ 
    public: 
    virtual void foo() 
    {} 
}; 

class Derived: public Base 
{ 
    public: 
    virtual void foo() 
    {} 
}; 

int main() 
{ 
    Base *pBase = NULL; 
    Base objBase; 
    Derived objDerived; 

    pBase = &objDerived; 
    pBase->foo(); 

    /*Here Derived class foo will be called, but i want this to call 
    a base class foo. Is there any way for this to happen? i.e. through 
    casting or something? */ 
} 

回答

29
pBase->Base::foo() 
7

你可以做到这一点通过范围解析操作::

事情是这样的:

pBase->Base::foo() 
10

上述两个反应是正确的...但要小心,如果你需要这样做,也许你有一个关于概念或设计的大问题...

+2

你说得对:从本质上讲,这意味着你需要两种方法。 – xtofl 2009-07-16 09:03:18

相关问题