Assuming that this is the real hierarchy, if Derived1
is a Base
derived class, must know how to contruct a parent class (Base
).
As I understood, maybe I'm wrong, if Derived1
is not really a Base
derived (child) class, and only needs to know it's interface, there is no need to know how to construct a Base
instance. So you can define Derived1 as follows:
struct Derived1
{
Derived1(int arg1, int arg2, int arg3, int arg4)
{}
.....
};
Only Base
derived classes need to know how to contruct Base
:
struct Derived2 : public Base
{
Derived2(int arg1, int arg2, int arg3, int arg4) :
Base(arg1, arg2, arg3, arg4)
{}
.....
};
Now, if Derived2
is really a Derived1
child, must know how to create it's parent. Previous code became:
struct Derived2 : public Base, public Derived1
{
Derived2(int arg1, int arg2, int arg3, int arg4) :
Base(arg1, arg2, arg3, arg4),
Derived1(arg1, arg2, arg3, arg4)
{}
.....
};
If you must use Base::BaseDoWork()
method from Derived1
, you may define classes as follows:
struct Base
{
Base(int arg1, int arg2, int arg3, int arg4)
{
std::cout << "Base::Base:" << arg1 << "," << arg2 << "," << arg3 << "," << arg4 << std::endl;
}
virtual void BaseDoWork()
{
std::cout << "Base::BaseDoWork" << std::endl;
}
};
struct Derived1
{
Derived1(int arg1, int arg2, int arg3, int arg4)
{}
template <typename T>
void DerivedDoWork(T & base) // only knows T interface
{
base.BaseDoWork();
}
};
struct Derived2 : public Base, public Derived1
{
Derived2(int arg1, int arg2, int arg3, int arg4) :
Base(arg1, arg2, arg3, arg4),
Derived1(arg1, arg2, arg3, arg4)
{}
virtual void DerivedDoWork()
{
Derived1::DerivedDoWork<Base>(*(static_cast<Base *>(this)));
}
};
Main keeps the same.
May you need to review this hierarchy.