0

I am inheriting from two classes. Both classes contain the same function name, but one being implemented and one being pure virtual. Is there any way that I can just use the one already implement?

#include <iostream>
using namespace std;


class BaseA {
public:
    void DoSomething() {
        value += 1;
        std::cout<<"DoSomething():"<< value<<endl;
    }

    int value;
};


class BaseB {
public:
      virtual void DoSomething() =0;
};


class Derived : public BaseA, public BaseB {
public:
    Derived() { value = 0; }

    // Compiler complains DoSomething() is not implemented.
    // Can we just use BaseA.DoSomething()?
};

int main() {
    Derived* obj = new Derived();
    obj->DoSomething();
}
Ruoyun Huang
  • 173
  • 10
  • The pure virtual is there for forcing you to implement. Why pure if you don't want to implemebt then? – Oblivion Jun 05 '19 at 18:49
  • This is to refactor legacy code. :-) The plan of introducing this BaseB is to provide an interface for unification purpose. – Ruoyun Huang Jun 05 '19 at 18:51
  • To answer my own question. After reading more posts, I found this: https://stackoverflow.com/questions/19528338/how-to-override-a-function-in-another-base-class – Ruoyun Huang Jun 05 '19 at 18:53

1 Answers1

0

You need to define a function because DoSomething is a pure-virtual function in BaseB class. But you can call the DoSomething from BaseA class in your implementation:

void DoSomething(){
    BaseA::DoSomething();
}
lucas.exe
  • 119
  • 3