Rectangle 27 1

c++ How to call a parent class function from derived class function?


class bottom : public left, public left { // Illegal
};
class left {
public:
    void foo();
};

class right {
public:
    void foo();
};

class bottom : public left, public right {
public:
    void foo()
    {
        //base::foo();// ambiguous
        left::foo();
        right::foo();

        // and when foo() is not called for 'this':
        bottom b;
        b.left::foo();  // calls b.foo() from 'left'
        b.right::foo();  // call b.foo() from 'right'
    }
};

@Mathai And that is why you aren't supposed to use using namespace std.

@bluesm: in classic OOP it makes no much sense, but in generic programming template<class A, class B> class C: public A, public B {}; can come to two types being the same for reasons depending on how your code is used (that makes A and B to be the same), may be two or three abstraction layer way from someone not aware of what you did.

I think it's useful to add, that this will call parent class method even if it is not implemented directly in the parent class, but is implemented in one of the parent classes in the inheritance chain.

I'll take the risk of stating the obvious: You call the function, if it's defined in the base class it's automatically available in the derived class (unless it's private).

If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...). You should note that unlike Java and C#, C++ does not have a keyword for "the base class" (super or base) since C++ supports multiple inheritance which may lead to ambiguity.

Incidentally, you can't derive directly from the same class twice since there will be no way to refer to one of the base classes over the other.

On a sidenote, it made me mad when i tried to put this in a cpp file. I had 'using namespace std'. 'left' is defined somewhere in that namespace. The example wouldn't compile - drove me crazy :) . Then I changed 'left' to 'Left'. Great example by the way.

Why would you like to inherit from the same class twice ?

Note
Rectangle 27 1

c++ How to call a parent class function from derived class function?


void child::print(int x)
{
    parent::print(x);
}

@ancajic: Using the names of classes from the question, parent is the actual name of the parent class. If the parent class were called widget, then you would call widget::print(x);.

@iheanyi Sounds interesting, but sorry, I'm not grasping it yet. Is foo() here analogous to print() or a separate function? And do you mean by using private inheritance to hide details inherited from the base, and providing public shadowing functions for things you do want to expose?

@underscore_d actually, its useful even if the base call was not interspersed with other logic. Let's say the parent class pretty much does everything you want, but exposes a method foo() you don't want users of child to use - either because foo() is meaningless in child or external callers to child will screw up what child is doing. So child may use parent::foo() in certain situations but provide an implementation of foo so that they hide parent's foo() from being called.

Of course, this would only be useful if the base call were interspersed with other logic, otherwise there'd be no point in overriding the function, so maybe it's a little too to-the-point ;)

Why does this anwer has so many upvotes? I don't see how this can be done in C++.

Note
Rectangle 27 1

c++ How to call a parent class function from derived class function?


// deriv_super.cpp
// compile with: /c
struct B1 {
   void mf(int) {}
};

struct B2 {
   void mf(short) {}

   void mf(char) {}
};

struct D : B1, B2 {
   void mf(short) {
      __super::mf(1);   // Calls B1::mf(int)
      __super::mf('s');   // Calls B2::mf(char)
   }
};

"Developers should know their compiler" this reasoning, and the inclusion of non standard features, is what led to IE6...

Eh, I'd prefer typdefing the parent as something like super.

I don't agree with Andrey: Developers should know the standard and should not need to bother with compiler features, if we consider writing software which is primarily compiler independent which I think is a good idea anyways because sooner or later in large projects multiple compilers are anyways used.

I won't try to justify usage of __super; I mentioned it here as an alternative suggestion. Developers should know their compiler and understand pros and cons of its capabilities.

I'd rather discourage anyone from using it, as it severely hinders portability of the code.

MSDN: Allows you to explicitly state that you are calling a base-class implementation for a function that you are overriding.

Note
Rectangle 27 1

c++ How to call a parent class function from derived class function?


void Base::FooBar()
{
   printf("in Base\n");
}

void ChildOfBase::FooBar()
{
  Base::FooBar();
}

If your base class is called Base, and your function is called FooBar() you can call it directly using Base::FooBar()

Note