Is there a way to ensure that only class Fabric
can construct the class Foo
and all of its sub-classes, without having to declare a private constructor and friend class Fabric
in each sub-class?
struct Foo {
friend class Fabric;
protected:
Foo() = default;
};
struct FooConcrete1 : Foo {
friend class Fabric;
protected:
Foo() = default;
};
Since friendship is not inherited, both manual actions when declaring each sub-class seem to be needed, which is error-prone.