0

Consider this code snippet:

#include <bits/stdc++.h>
using namespace std;

class Mammal;
class Operation;

class Animal{
    public:
        virtual void doOp(Operation* op) = 0;
};

class Mammal{
    public:
        void doOp(Operation* op){
            op->thisIsMammal(this);
        }
};

class Pig:  public Mammal, public Animal{

};

class Operation{
    public: 
        void thisIsMammal(Mammal* mammal){
            cout << "This is a mammal!" << endl;
        }
};

int main(){
    Pig  pig;
    Operation op;
    pig.doOp(&op);
    return 0;
}

There is a virtual function declared in Animal class that needs to be redefined in Pig class. However, Pig derives from Mammal which has the definition of this function

I still get an error that the function has not been redefined in the derived class. Is there any way to make the definition in Mammal sufficient?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Mahathi Vempati
  • 1,238
  • 1
  • 12
  • 33
  • No, C++ does not work this way. You have to declare the virtual function in the subclass, and have the subclass's function explicitly call the implementation in the implementing superclass. – Sam Varshavchik Nov 07 '19 at 18:41
  • Perhaps [this](https://stackoverflow.com/q/58208774/10957435) is what's going on? –  Nov 07 '19 at 18:50
  • You may also want to research/use the [override specifier](https://en.cppreference.com/w/cpp/language/override). – Jesper Juhl Nov 07 '19 at 18:53

2 Answers2

2

However, Pig derives from Mammal which has the definition of this function

No, it doesn't. Mammal does not override the function Animal::doOp because Mammal does not inherit Animal.

A simple solution is to derive Mammal from Animal, and remove the Animal base from Pig:

class Mammal : public Animal {

class Pig :  public Mammal {
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • See, and I was going to suggest virtual inheritance. I like this better. –  Nov 07 '19 at 18:51
  • 1
    @Chipster virtual inheritance is a *very sharp* tool that should almost *never* be used, and *if* used, it should only be done by experts who *really* know what they are doing (IMHO). – Jesper Juhl Nov 07 '19 at 18:57
  • @JesperJuhl What is the most significant hazard w/ virtual inheritance? – curiousguy Nov 08 '19 at 07:51
  • It is very difficult to decide what is worse - multiple inheritance as in the question, or multiple inheritance as in your answer. – BЈовић Nov 08 '19 at 08:15
0

Mammal in your code does not derive from Animal, so it can't really override the virtual function Animal::doOp(). The definition of the member function Mammal::doOp() in your code just defines a non-virtual function named doOp(). It overrides nothing.

Consider using the override specifier (since C++11).

If you had used the override specifier when you thought you were overriding doOp() in Mammal, you would have got a compilation error at this stage since the following member function definition does not override doOp():

class Mammal{
    public:
        void doOp(Operation* op) override {
            op->thisIsMammal(this);
        }
};
JFMR
  • 23,265
  • 4
  • 52
  • 76