c++ - 运行父类(super class)覆盖函数

标签 c++ inheritance subclass super

如何从子类中覆盖的函数调用父类(super class)中覆盖的函数?

例如:super 类有一个名为 foo 的函数,它在一个名为 sub 的子类中被重写,如何让 subs foo 调用 supers foo?

最佳答案

您可以利用继承!

class A
{
public:
    virtual void Foo()
    {
        // do some base class foo-ing
    }
};

class B : public A
{
public:
    virtual void Foo()
    {
        // do some subclass foo-ing

        // to call the super-class foo:
        A::Foo( );
    }
};

void main()
{
    B obj;
    obj.Foo( );

    // This is if you want to call the base class Foo directly using the instance obj
    A* handle = &obj;
    handle->Foo( );
}

关于c++ - 运行父类(super class)覆盖函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18473497/

相关文章:

c++ - 如何正确地将 void * 值转换为 CString

c++ - 在 C++ 中平衡二维数组

c++ - 重构 C++ 代码以使用脚本语言?

C++多态构造函数错误;标识符未定义

python - 封装 TensorFlow 计算图的类中的类继承

Java子类化: what is the method that will get triggered?

java - 在Java中携带这些变量有什么问题吗?

c++ - 这个 while 循环是做什么的?

c++ - 为什么是 "an inherited constructor is not a candidate for initialization from an expression of the same or derived type"?

iphone - UITextField 上的 drawTextInRect 未调用