C++ – Initializing Derived Classes Consistently

cinheritanceinitializationunit testing

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:

template <typename ConcreteT> class Mock {
    initMock() {
        static_cast<ConcreteT *>(this)->x = 1;
        static_cast<ConcreteT *>(this)->y = "abc";
        static_cast<ConcreteT *>(this)->z = 0.5;
    }
};

class MockA : A, Mock<MockA> { ... };
//                    ^^^^^ specialized on the type that inherits it, that's the point

Here it would make things simple to inherit A through the Mock class though, so

template <typename BaseT> class Mock : public BaseT {
    initMock() {
        x = 1;
        y = "abc";
        z = 0.5;
    }
};

class MockA : Mock<A> { ... };
//                 ^ just the base class

That does away with the ugly static_casts, but does not allow you to provide methods in MockA that will be called by Mock. But you can have both...

template<typename ConcreteT, typename BaseT>
class Mock : public BaseT { ... };

class MockA : Mock<MockA, A> { ... };

Now Mock can call methods of MockA on itself via static cast and call methods of A (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.

Related Topic