413

Say I have classes Foo and Bar set up like this:

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;
    }
};

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++?

Sled
  • 18,541
  • 27
  • 119
  • 168
Alex
  • 14,973
  • 13
  • 59
  • 94
  • 1
    possible duplicate of [virtual function call from base class](http://stackoverflow.com/questions/397404/virtual-function-call-from-base-class) – Vladimir F Героям слава Dec 02 '14 at 19:38
  • 1
    For Googlers: note that you may have issues like I did with storing it as a class member variable that's not a pointer. See my answer here: http://stackoverflow.com/questions/4798966/c-calling-completely-wrong-virtual-method-of-an-object/42429076#42429076 I involved new/delete to fix. – Andrew Feb 24 '17 at 01:14
  • The Decorator design pattern is based on this :) – LRDPRDX Jan 21 '23 at 19:41

8 Answers8

535

In C++ you have to explicitly name the base class in calling the derived class method. This can be done from any method from the derived class. The override is a special case of the method of the same name. In Java there is no multi inheritance, so you can use super which will uniquely name the base class. The C++ syntax is like this:

class Bar : public Foo {
  // ...

  void printStuff() override {  // help the compiler to check
    Foo::printStuff(); // calls base class' function
  }
};
Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
sth
  • 222,467
  • 53
  • 283
  • 367
  • 14
    Is there any possible issues with doing this? Is it bad practice? – Robben_Ford_Fan_boy Feb 02 '11 at 15:01
  • 42
    @David: No, it's perfectly normal to do this, though it might depend on your actual class if it is actually useful. Only call the base class method if does something you want to happen ;). – sth Feb 02 '11 at 15:13
  • What is the syntax, if I want to "call" a base class' implementation of an overloaded operator (which in my case is virtual)? – not-a-user Nov 27 '13 at 11:20
  • 29
    careful this is a __code smell__ when your clients becomes REQUIRED to do it ! (called `call super requirement`, check it out here: http://en.wikipedia.org/wiki/Call_super) – v.oddou Mar 28 '14 at 03:49
  • 16
    @v.oddou: There is nothing wrong with calling the super class when it's useful. That page you linked is just about some specific potential problem involving inheritance. It even says itself that there is nothing wrong with calling the super class in general: *"Note that it is the **requirement** of calling the parent that is the anti-pattern. There are many examples in real code where the method in the subclass may still want the superclass's functionality, usually where it is only augmenting the parent functionality."* – sth Mar 28 '14 at 11:49
  • 2
    @sth: yes, this is why I wrote required in all caps. The wiki link was inspired to refect this article by Martin Fowler : http://www.martinfowler.com/bliki/CallSuper.html – v.oddou Mar 31 '14 at 00:24
  • 33
    It may be obvious to most, but for completeness, remember to never do this in constructors and destructors. – TigerCoding Jul 06 '14 at 22:23
  • 2
    Regarding @Javy, to execute the baseclass constructor with a parameter the following syntax can be used `constructor(int param) : baseclass(param) { your code ... }`. It is important to remember that objects are constructed bottom up, meaning baseclass constructors are called first. Also see [here](http://stackoverflow.com/a/120916/258418) – ted Dec 02 '14 at 10:39
  • 3
    To clarify what was said above, I'd say it's a code smell when calling the base class method is part of your public interface. Doing this internally, though, shouldn't be a problem (i.e. among a set of classes that you define together in a single module). – Nate C-K Jan 29 '15 at 19:54
  • have not problem using of `::` for calling a `non-static` method? – BattleTested_закалённый в бою Sep 06 '16 at 08:19
  • 1
    @MohammadrezaPanahi in this case, it assumes that the current class is passed as the "this" parameter. (Or rather, that the current environment satisfies the requirements for the super class's functions to be called. – violet_white Sep 15 '16 at 01:21
  • 3
    @TigerCoding why not in constructors? by the time the derived class gets to call anything, construction of the base has already been completed, so calling a base method is safe. (destructors have the opposite order, so for them, the advice works) – underscore_d Sep 18 '16 at 07:24
  • I had a case when I redeclared a method from a base class to only add a __declspec(dllexport) attribute. And I call the base class::methodImpl() in my implementation. – Mykola Jan 22 '18 at 12:59
  • 1
    Please do not gloss over the important comments by NateC-K, v.oddou, and sth. I've come across confusion about this several times insofar as what best practices are. To reiterate: there isn't anything necessarily wrong with calling a base class method internally within a set of related classes/etc., but making it a _public_ requirement is usually a sign of bad design. I.e., try to avoid making users have to worry about running into comments like, "Be sure to call Foo::printStuff() if you override this." – Dan Apr 30 '18 at 03:14
  • @TigerCoding: no problem to do base calls on ctor or dtor (23 votes on your coment ?!?!?) – Isaac Pascual Jan 21 '19 at 19:13
  • What if the base class method is private? – flarn2006 Aug 14 '20 at 02:29
137

Yes,

class Bar : public Foo
{
    ...

    void printStuff()
    {
        Foo::printStuff();
    }
};

It is the same as super in Java, except it allows calling implementations from different bases when you have multiple inheritance.

class Foo {
public:
    virtual void foo() {
        ...
    }
};

class Baz {
public:
    virtual void foo() {
        ...
    }
};

class Bar : public Foo, public Baz {
public:
    virtual void foo() {
        // Choose one, or even call both if you need to.
        Foo::foo();
        Baz::foo();
    }
};
Alex B
  • 82,554
  • 44
  • 203
  • 280
83

Sometimes you need to call the base class' implementation, when you aren't in the derived function...It still works:

struct Base
{
    virtual int Foo()
    {
        return -1;
    }
};

struct Derived : public Base
{
    virtual int Foo()
    {
        return -2;
    }
};

int main(int argc, char* argv[])
{
    Base *x = new Derived;

    ASSERT(-2 == x->Foo());

    //syntax is trippy but it works
    ASSERT(-1 == x->Base::Foo());

    return 0;
}
Netherwire
  • 2,669
  • 3
  • 31
  • 54
AlwaysTraining
  • 2,039
  • 18
  • 12
  • 2
    Note that you need both `x` to be of type `Base*` and to use the `Base::Foo` syntax for this to work – TTimo Mar 21 '18 at 19:59
29

Just in case you do this for a lot of functions in your class:

class Foo {
public:
  virtual void f1() {
    // ...
  }
  virtual void f2() {
    // ...
  }
  //...
};

class Bar : public Foo {
private:
  typedef Foo super;
public:
  void f1() {
    super::f1();
  }
};

This might save a bit of writing if you want to rename Foo.

MartinStettner
  • 28,719
  • 15
  • 79
  • 106
  • 1
    Fun way to do it, but will not work with multiple inheritance. – Mad Physicist Oct 17 '14 at 20:43
  • 7
    And what if you have more than 1 base class? Anyway, it is silly and often futile to try to bend C++ to look like some other language. Just remember the base's name and call it by that. – underscore_d Apr 11 '16 at 13:18
6

If you want to call a function of base class from its derived class you can simply call inside the overridden function with mentioning base class name(like Foo::printStuff()).

code goes here

#include <iostream>
using namespace std;

class Foo
{
public:
    int x;

    virtual void printStuff()
    {
         cout<<"Base Foo printStuff called"<<endl;
    }
};

class Bar : public Foo
{
public:
    int y;

    void printStuff()
    {
        cout<<"derived Bar printStuff called"<<endl;
        Foo::printStuff();/////also called the base class method
    }
};

int main()
{
    Bar *b=new Bar;
    b->printStuff();
}

Again you can determine at runtime which function to call using the object of that class(derived or base).But this requires your function at base class must be marked as virtual.

code below

#include <iostream>
using namespace std;

class Foo
{
public:
    int x;

    virtual void printStuff()
    {
         cout<<"Base Foo printStuff called"<<endl;
    }
};

class Bar : public Foo
{
public:
    int y;

    void printStuff()
    {
        cout<<"derived Bar printStuff called"<<endl;
    }
};

int main()
{

    Foo *foo=new Foo;
    foo->printStuff();/////this call the base function
    foo=new Bar;
    foo->printStuff();
}
Tunvir Rahman Tusher
  • 6,421
  • 2
  • 37
  • 32
3

If there are multiple levels of inheritance, you can specify the direct base class, even if the actual implementation is at a lower level.

class Foo
{
public:
  virtual void DoStuff ()
  {
  }
};

class Bar : public Foo
{
};

class Baz : public Bar
{
public:
  void DoStuff ()
  {
    Bar::DoStuff() ;
  }
};

In this example, the class Baz specifies Bar::DoStuff() although the class Bar does not contain an implementation of DoStuff. That is a detail, which Baz does not need to know.

It is clearly a better practice to call Bar::DoStuff than Foo::DoStuff, in case a later version of Bar also overrides this method.

Phil Jollans
  • 3,605
  • 2
  • 37
  • 50
0

check this...

#include <stdio.h>

class Base {
public:
   virtual void gogo(int a) { printf(" Base :: gogo (int) \n"); };    
   virtual void gogo1(int a) { printf(" Base :: gogo1 (int) \n"); };
   void gogo2(int a) { printf(" Base :: gogo2 (int) \n"); };    
   void gogo3(int a) { printf(" Base :: gogo3 (int) \n"); };
};

class Derived : protected Base {
public:
   virtual void gogo(int a) { printf(" Derived :: gogo (int) \n"); };
   void gogo1(int a) { printf(" Derived :: gogo1 (int) \n"); };
   virtual void gogo2(int a) { printf(" Derived :: gogo2 (int) \n"); };
   void gogo3(int a) { printf(" Derived :: gogo3 (int) \n"); };       
};

int main() {
   std::cout << "Derived" << std::endl;
   auto obj = new Derived ;
   obj->gogo(7);
   obj->gogo1(7);
   obj->gogo2(7);
   obj->gogo3(7);
   std::cout << "Base" << std::endl;
   auto base = (Base*)obj;
   base->gogo(7);
   base->gogo1(7);
   base->gogo2(7);
   base->gogo3(7);

   std::string s;
   std::cout << "press any key to exit" << std::endl;
   std::cin >> s;
   return 0;
}

output

Derived
 Derived :: gogo (int)
 Derived :: gogo1 (int)
 Derived :: gogo2 (int)
 Derived :: gogo3 (int)
Base
 Derived :: gogo (int)
 Derived :: gogo1 (int)
 Base :: gogo2 (int)
 Base :: gogo3 (int)
press any key to exit

the best way is using the base::function as say @sth

  • As explained in [this Question](https://stackoverflow.com/questions/9661936/inheritance-a-is-an-inaccessible-base-of-b), this shouldn't work because of `protected` inheritance. To cast a base class pointer you have to use public inheritance. – Darkproduct Jun 03 '20 at 09:06
  • Interesting. After reading [this answer](https://stackoverflow.com/a/9661959/6411540) I thought with protected inheritance the fact that Derived is derived from Base would be only visible to the class itself and not visible to the outside too. – Darkproduct Jun 12 '20 at 10:40
0

Yes you can call it. C++ syntax for calling parent class function in child class is

class child: public parent {
  // ...

  void methodName() {
    parent::methodName(); // calls Parent class' function
  }
};

Read more about function overriding.

Atif
  • 77
  • 1
  • 2
  • 9