When writing a mixin the objective is generally to make it maximally reusable. Often the best way to achieve that aim is to avoid any state (i.e. member variables)! Consider a mixin that is responsible for drawing a slider
$mixin DrawSliderMixin
{
void Draw() const
{
float pos = GetThumbPos();
int height = GetHeight();
int width = GetWidth();
// Draw the slider in a rectangle of
// dimensions ’width’, ’height’
// with the thumb at position ’pos’
...
}
};
Notice that this mixin assumes that the base class has implemented the following functions
int GetWidth() const;
int GetHeight() const;
float GetThumbPos() const;
We have already seen how mixins can help implement GetWidth() and GetHeight(). For example
$mixin DefaultSliderDimensionsMixin
{
int GetWidth() const { return 128; }
int GetHeight() const { return 32; }
};
Now consider the following class
$class X isa ceda::IView
model
{
int x;
}
mixin
[
DefaultSliderDimensionsMixin
// Anonymous mixin
{
float GetThumbPos() const
{
return (float) x/100;
}
}
DrawSliderMixin
]
{
};
This makes use of an anonymous mixin within the mixin chain. The anonymous mixin implements the method GetThumbPos() that is expected by DrawSliderMixin. The result is that the slider position is determined by the data model variable x.