Initializing derived classes in the same way

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, …?

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.

4

Well, you could introduce a static method which returns a default set of all protected member values and fetch them in each init method into the fields of the mock objects.

Edit:

MockA::init()
{
  initMock(MockInitializer.getDefaultValues());
  //go on with your code
}

MockA::initMock(anystruct def_values)
{
  //assign your values
}

The problem is that in any place you’ll have to do the assignment, why not do it in its own method?

5

Here is how i would to that if I understand your problem :


class Base
{
protected:
    initMock()
    {
        x = 1;
        y = "abc";
        z = 0.5;
    }
};

class A { /* Whatever you want ... */ };

class B { /* Whatever you want ... */ };

class MockA : public A, public Base
{
public:
    initMock()
    {
         Base::initMock();
    }

};

And you can do the same thing with MockB.

10

If Base is the common base class of A, B, C holding protected members Base::x, Base::y, etc., then I guess there are accessor methods like

Base::SetX(type_x xval){x=_val;}

etc. So write a static function in a helper class MockInit

void MockInit::initForTests(Base &b)
{
    b.SetX(1);
    b.SetY("abc");
    b.SetZ(0.5);
}

and call that function this way:

MockA::init()
{
     MockInit::initForTests(*this);
}

MockB::init()
{
     MockInit::initForTests(*this);
}

That’s it – simple and stupid. However, if there are no such accessor methods, and you have you have absolutely no access to x,y,z from outside the inheritance hierarchy, I would consider changing the initial design of Base for better testability. At least, I would consider to make MockInit a friend class of Base.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật