I have a class Base that has several children, say A, B, C. For testing purposes I'd like to mock those derived classes by deriving from them. So MockA derives from A, MockB derives from B and so on.
The problem is, that MockA, MockB, … all inherit protected members from Base that have to be set in the same way in each mock class. The code looks like this:
MockA::init()
{
x = 1; // inherited from Base
y = "abc"; // inherited from Base
z = 0.5; // inherited from Base
}
MockB::init()
{
x = 1; // inherited from Base
y = "abc"; // inherited from Base
z = 0.5; // inherited from Base
}
and so on.
So my question is, how can I avoid this cut&paste initialization? Can I achieve it without touching Base, A, B, C, …?
Best Answer
You want a "mixin". In C++ they are usually implemented with templates and specifically using CRTP, the "curiously recurring template pattern". It might be an overkill if it's just a bit of common code, than Pierre's answer seems most appropriate. But as the amount of common code for the mock classes grows, so will value of template.
Basic CRTP would go like this:
Here it would make things simple to inherit A through the Mock class though, so
That does away with the ugly static_casts, but does not allow you to provide methods in
MockA
that will be called byMock
. But you can have both...Now
Mock
can call methods ofMockA
on itself via static cast and call methods ofA
(or rather the ultimate base) directly.Note: The mixin templates are basically the same thing as mixins in Ruby or interfaces with method implementations introduced in Java 8. Those languages it is a special construct, because they don't have anything of the power of C++ templates, but the implementation is similar—an intermediate class is constructed by compiler and injected in the hierarchy.