如果我覆盖它,我可以调用一个基类的虚拟函数吗? [英] Can I call a base class's virtual function if I'm overriding it?

查看:98
本文介绍了如果我覆盖它,我可以调用一个基类的虚拟函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有类 Foo Bar 设置如下:

class Foo
{
public:
    int x;

    virtual void printStuff()
    {
        std::cout << x << std::endl;
    }
};

class Bar : public Foo
{
public:
    int y;

    void printStuff()
    {
        // I would like to call Foo.printStuff() here...
        std::cout << y << std::endl;
    }
};

如代码中所注释的,我想能够调用基类的函数,最重要。在Java中有 super.funcname()语法。

As annotated in the code, I'd like to be able to call the base class's function that I'm overriding. In Java there's the super.funcname() syntax. Is this possible in C++?

推荐答案

C ++语法如下:

class Bar : public Foo {
  // ...

  void printStuff() {
    Foo::printStuff(); // calls base class' function
  }
};

这篇关于如果我覆盖它,我可以调用一个基类的虚拟函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆