On 23 October 2012 16:05, Klaim - Joël Lamotte <
mjk...@gmail.com> wrote:
> I'm designing a class, which name and purpose gives the false assumption
> that both inheritance and composition might be good usage of it, but it's
> designed to be used for composition only. Therefore, any way better
> than comments to document this is good. Therefore, using final class
> seems a good idea.
Do remember that private inheritance is a form of composition. Also remember
that having a construction veneer that inherits from a non-polymorphic base
to add new constructors and other such state is another valid use case. For
those reasons, I find it dubious to prevent inheritance completely.
I would like to have facility that makes the virtual functions in a class final,
but allows inheritance. You can't achieve that by just overriding every
virtual and marking them final, because you might want to do that for
a dependent base of a class template. I plan to do an experimental
implementation of a final_overrider keyword and potentially create
a proposal for it. Example:
struct AbstractBase { virtual void f() = 0;};
struct ConcreteBase : AbstractBase {void f();};
template <class T> struct X FinalOverrider : T
{
};
final_overrider<AbstractBase> X1; // ill-formed, can't have final
overriders if there are non-overridden pure virtuals left
final_overrider<ConcreteBase> X2; // ok
struct FurtherOverrider : FinalOverrider<ConcreteBase> // inheritance ok
{
void f(); // ill-formed, FinalOverrider is the final overrider,
and f() is final from that point onwards
};
and then, also, with multiple inheritance
struct AnotherConcreteBase {virtual void g();};
struct FurtherOverrider2 : AnotherConcreteBase, FinalOverrider<ConcreteBase>
{
void g(); // ok, we can still override functions from the base
that allows it
void f(); // ill-formed, the FinalOverrider 'branch' of the bases
is the final overrider
};