Re: Inheriting the Wrong Constructor
Read the FAQ. Prefer initialisation to assignment. Also, it would
seem that both constructors can be merged:
base(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z=
) {}
(and learn to omit the semicolons after function bodies).
This has nothing to do with inheriting constructors, and everything with
the rule that the _most derived class_ is responsible for doing the
initialisation of the virtual base class[es].
'derived' has 'base' as virtual base class. That means it *itself* is
responsible for initialising the 'base' subobject. That means, in turn=
,
that in the initialiser list of 'derived' there *will be* the call to
'base::base()' (implicitly) if you don't specify explicitly what
constructor should be used. It is up to you to do it right, the
'derived' object cannot delegate the responsibility to any of its other
base classes. So, you ought to write
derived() : base(1,2,3) {}
if you wanted the particular values to be passed to 'base::base'.
Thanks for the response but it's not the answer I was looking for. I
don't want the answer I can't or my design in insufficient, I want the
answer do x y z to get around it. So what is my reason to want the
base class initialized by an intermediate class rather than the
derived class? Because I'm using a policy based design. Let me
give you a more refined example, and again this is example code so no
need to worry about other critiques (I knew about all those and I read
the FAQ).
enum eShape {
circle,
triangle,
square
};
struct baseInteface
{
eShape shape;
baseInteface() {};
baseInteface(eShape shape) {base::shape = shape;};
//other stuff related to interface
};
namespace Triangle {
struct InitPolicy : public virtual baseInteface
{
InitPolicy() : baseInteface(triangle){};
};
struct SomeOtherPolicy {};
};
namespace Square {
struct InitPolicy : public virtual baseInteface
{
int height, width;
InitPolicy() : baseInteface(square){height = 0; width = 0;};
};
struct SomeOtherPolicy {};
};
template <
class shapePolicy,
class otherPolicy
struct Shape : public virtual shapePolicy, public virtual otherPolicy
{
Shape() : shapePolicy() {};
};
int main()
{
Shape<Triangle::InitPolicy, Triangle::SomeOtherPolicy> shape;
return 0;
}
Here, your suggestion of writing derived() : baseInteface(/*policy
data*/) {} violates my encapsulation, my derived class should not have
knowledge of the implementation of any of the policies, baseInteface
is the interface and derived is just the template conglomerator.
OK so you might ask me why don't you throw out all those policies and
intermediate classes and just have class Triangle inherit from class
Polygon? Because this is an example, I have too many derivations and
a policy based design cuts that number down. Or you might ask why not
just bite the bullet and use a virtual OnInit function? Well that's
what I tried at first, but C++ cannot call virtual functions from
farther down the inheritance tree in a constructor, because the object
further down doesn't exist yet!
So any ideas on how to have some intermediate policy init a base class
without the derived class knowing about it?