To call the virtual function of the parent class

Asked 2 years ago, Updated 2 years ago, 91 views

In Bar, Foo's virtual void printStuff is override I'd like to sing printStuff() of Foo. In Java, we could've used super.printStuff() together How do I use it in C++?

class Foo
{
public:
    int x;

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

class Bar : public Foo
{
public:
    int y;

    void printStuff()
    {
        // I want to sing Foo.printStuff()!!!!!
        std::cout << y << std::endl;
    }
};

c++ virtual-functions overriding

2022-09-21 18:25

1 Answers

In C++, the name of the base class is followed by :<:/code> as follows:

class Bar : public Foo {
  // ...

  void printStuff() {
    Foo::printStuff(); // call function in base class
  }
};


2022-09-21 18:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.