Alex Alex - 1 year ago 71
C++ Question

Can I call a base class's virtual function if I'm overriding it?

Say I have classes

set up like this:

class Foo
int x;

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

class Bar : public Foo
int y;

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

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
syntax. Is this possible in C++?

sth sth
Answer Source

The C++ syntax is like this:

class Bar : public Foo {
  // ...

  void printStuff() {
    Foo::printStuff(); // calls base class' function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download