Two base classes with the same pure virtual function
Asked Answered
U

1

7

I have a class A that was inherited from class B. So the interface of class A contains some pure virtual functions of class B and some functions of class A. Now I need to make unit tests for class A, so wanna have some interface for class A that I can mock. So now I'm wondering if the given code is correct in C++14 and can it lead to UB:

class Base1 {
public:
  virtual void func() = 0;
};

class Base2 {
public:
  virtual void func() = 0;
};

class Derived : public Base1, public Base2 {
public:
  void func() override { }
};

int main() {
  Derived d;
  d.func();
  return 0;
}
Unipolar answered 16/2, 2018 at 12:55 Comment(12)
Welcome on SO. We are happy to help if you show that you have tried your best and got stuck. So what have you researched? Have you tried to compile this code?Proustite
Possible duplicate of C++ inherit from multiple base classes with the same virtual function nameTravistravus
@Travistravus almost a dupe in the question. The first answer to that post definitely answers this one too.Proustite
The code compiles and runs on my machine. It is not undefined behavior. Arlo Belshee points out that mocks for unit tests is a code smell that can help improve design by eliminating the need for having a mock.Lemmon
@Lemmon Can you provide a link to this quote from Belshee?Proustite
@Proustite • I used to work with Arlo Belshee. He has strong (and I think good) opinions on things like agile, TDD, programming principles, et cetera. arlobelshee.com/the-no-mocks-bookLemmon
@Lemmon How relevant is this for C++?Proustite
I've tried to compile it and it is working. But I dunno if it UB or not.Unipolar
I've already read the answer of Melebius, but it is not what I need.Unipolar
@Proustite • it's relevant for any language in which one does unit testing, and employs mocks. C++ is not the friendliest language for unit testing, nor for mocks. There's another position on unit testing in general by James Coplien (who is eminently familiar with C++), in his essay "Why Most Unit Testing is Waste" -- not my position, but interesting and I have tons of respect for James. Personally, I wish C++ had good contract support like D does.Lemmon
@Lemmon That the code compiles and runs on your machine does not mean it's not UB, actually that behavior is included in the concept of UB.Valerivaleria
@Valerivaleria • I did not mean that because the code compiles & runs on my machine, it is not undefined behavior. I was stating the OP's code is not undefined behavior.Lemmon
P
6

Yes, this code is well-formed and void func() overrides both A::func() and B::func(). From the C++14 standard:

[class.virtual]

  1. If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.
Proustite answered 16/2, 2018 at 13:33 Comment(1)
@EdvardDavtyan If this answers your question, you should accept it (the green tick)Proustite

© 2022 - 2024 — McMap. All rights reserved.