-1

I have a derived class inheriting from both an interface and a base class. The interface defines a virtual function GetId, which is implemented in the other base class.

class ITestClient
{
  public:
    virtual int GetId() = 0;
};

class BaseClient
{
  public:
    int GetId();
}

int BaseClient::GetId()
{
  return 10;
}

class TestClient : public ITestClient, public BaseClient {
};

I get unimplemented pure virtual method 'GetId'. So I could do this to fix the compile error...

class TestClient : public ITestClient, public BaseClient {
  int GetId()
  {
    return BaseClient::GetId();
  }
};

Why isn't the base class BaseClient's concrete function definition of GetId sufficient as an implementation of the interface's virtual function?

yomikaze
  • 19
  • 2
  • 1
    Why not just have `BaseClient` derive from `ITestClient`? –  Jun 15 '17 at 23:56
  • 1
    Are you asking why the language is defined that way? Or do you think there is a bug in the compiler? – R Sahu Jun 16 '17 at 00:01
  • 1
    Imagine the compiler worked the way you expect. What should happen if you derive from yet another class, `Unrelated`, that also happens to have a method named `GetId()` (which was not intended to have anything to do with `ITestClient`; say it's coming from a third-party library)? Worse still, imagine that `Unrelated` didn't originally have `GetId()` method, and your program worked fine; but later the third-party authors added a private `GetId()` for their own reasons. C++ has enough problems with [fragile base classes](https://en.wikipedia.org/wiki/Fragile_base_class) as is. – Igor Tandetnik Jun 16 '17 at 00:57
  • 1
    Or consider this: `class Base { virtual int GetId() { return 42; } }; class Unrelated {}; class Derived : public Base, public Unrelated {};` So far so good: `Derived d; d.GetId();` would return 42. Now, someone adds `private: int GetId() { return -1; }` to `Unrelated`. What should happen, in your ideal world? Should it be an error? Should `Unrelated::GetId()` be ignored? Or should `d.GetId()` silently change meaning and start returning -1 where before it returned 42? – Igor Tandetnik Jun 16 '17 at 01:05
  • @RSahu yeah, I was confused about c++ multiple inheritance and wondering why the language is defined this way – yomikaze Jun 16 '17 at 14:22
  • 1
    @IgorTandetnik that makes sense thanks. I had a wrong assumption about inheritance – yomikaze Jun 16 '17 at 14:25

1 Answers1

0

Not sure I understand the question. BaseClient is-not-A ITestClient (is-A relationship is the inheritance property required). So the definition in BaseClient cannot be an implementation of that interface function.

The son of a friend of yours won't start looking like you just because you take him along in your car, your own son however will. That's the equivalent relation.

Navie
  • 164
  • 1
  • 8
  • I see. I figured that since BaseClient and ITestClient are all "in the same car" together, the compiler could get them to get along with each other. – yomikaze Jun 16 '17 at 14:26
  • In Java the equivalent code works exactly how the asker thinks it should. So the answer needs to be specific to C++. – user253751 Jun 18 '17 at 21:57
  • Sure, as the code is C++ and his tags are C++/C++11 though, I saw no reason to think the question was unrelated to C++. – Navie Jun 19 '17 at 07:19