[ad_1]
At the moment I have an inheritance structure like this:
class B
{
virtual void f() = 0;
};
class D1 : public B
{
void f() override { }
};
class D2 : public B
{
void f() override { }
};
class D3 : public D2
{
D1* m_d1; // USUALLY available, SOMETIMES not
void f() override
{
if(m_d1)
{
m_d1->f();
}
else
{
D2::f();
}
}
};
I can live with the approach at the moment, if relevant the types are detected by a parallel hierarchy and I can simply static_cast
the types from B
if needed – still a D3
actually shouldn’t be identifiable as a D2
(e.g. via dynamic_cast
) – it’s just not clean, and could be prevented e.g. by doing
class D2 : public virtual B
{
};
class D3 : public virtual B, private D2
{
};
which would then break the static_cast
s, though. I’ve already thought about an aggregated member of an implementation type:
class D2 : public B
{
Impl m_impl;
void f() override { m_impl.f(); }
};
// D3 analogously
which, though, requires duplicating the interface yet once more (there’s actually a whole bunch of functions to consider…) and the Impl
class gets visible as well (though still appears cleanest approach to me at the moment, so likely going to switch to).
Well aware that this isn’t possible I’d really have appreciated inheriting publicly only D2
‘s own B
part and the rest private – is there yet another technique or clever trick to get close to something alike or something cleverer as the aggregated implementation?
[ad_2]