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

8

422

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

Idle answered 23/3, 2009 at 6:17 Comment(3)
possible duplicate of virtual function call from base classCellar
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: #4799466 I involved new/delete to fix.Cloudscape
The Decorator design pattern is based on this :)Dalliance
A
546

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
  }
};
Andreeandrei answered 23/3, 2009 at 6:20 Comment(16)
Is there any possible issues with doing this? Is it bad practice?Cushing
@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 ;).Andreeandrei
What is the syntax, if I want to "call" a base class' implementation of an overloaded operator (which in my case is virtual)?Naze
careful this is a code smell when your clients becomes REQUIRED to do it ! (called call super requirement, check it out here: en.wikipedia.org/wiki/Call_super)Cryometer
@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."Andreeandrei
@sth: yes, this is why I wrote required in all caps. The wiki link was inspired to refect this article by Martin Fowler : martinfowler.com/bliki/CallSuper.htmlCryometer
It may be obvious to most, but for completeness, remember to never do this in constructors and destructors.Enid
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 hereTabithatablature
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).Kyles
have not problem using of :: for calling a non-static method?Logogram
@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.Baluchistan
@Enid 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)Stalemate
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.Aryan
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."Hord
@TigerCoding: no problem to do base calls on ctor or dtor (23 votes on your coment ?!?!?)Addressograph
What if the base class method is private?Esoteric
T
140

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();
    }
};
Tabitha answered 23/3, 2009 at 6:20 Comment(3)
This is a better answer than the selected one. Thanks.Barbarian
Multiple inheritance? Ooh yikes!Anaglyph
C++ is crazy , I am almost too after 4 days of dealing with it... If I imagine to write some really complex code as I do in kotlin in C++ I better change my hobby to gradeningElane
T
87

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;
}
Twigg answered 12/6, 2011 at 1:42 Comment(1)
Note that you need both x to be of type Base* and to use the Base::Foo syntax for this to workGenuine
A
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.

Apophthegm answered 23/3, 2009 at 15:11 Comment(2)
Fun way to do it, but will not work with multiple inheritance.Barbarian
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.Stalemate
G
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();
}
Gossamer answered 28/5, 2013 at 14:20 Comment(0)
P
4

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.

Perdomo answered 23/12, 2020 at 12:14 Comment(0)
J
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

Janettejaneva answered 16/11, 2016 at 10:29 Comment(2)
As explained in this Question, this shouldn't work because of protected inheritance. To cast a base class pointer you have to use public inheritance.Bankable
Interesting. After reading this answer 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.Bankable
G
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.

Garcia answered 17/2, 2018 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.