c++ - 如何通过指向派生类的基类指针调用基类方法

标签 c++ polymorphism

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? */
}

最佳答案

pBase->Base::foo()

关于c++ - 如何通过指向派生类的基类指针调用基类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1136249/

相关文章:

提升映射器或记录框架中的继承

Java 多态性 : Accessing methods in super and subclasses

c++ - 使用 ptr_vector 时访问派生类的方法

oop - 什么是多态性、它的用途是什么以及如何使用它?

C++:第 1 步:ExtractIconEx。第2步: ???第 3 步:设置菜单项位图

c++ - assert 语句在 C++ 中不起作用

c++ - Rust 中有类似表达式模板的东西吗?

compiler-errors - SELECT TYPE 构造中的多态性分配

c++ - 介绍 "auto"的同义词

c++ - 在 64 位机器上编译 32 位 matlab 应用程序 (c++)