Assuming that there is a class A
.
I want my class to derived from A
twice, in order to manage two A
segment and visit their protected methods.
Like:
typedef A yetA;
class D: public A, public yetA {};
This doesn't work. Is there a method to do that?
CodePudding user response:
First off all... I'd caution you to rethink this design, because (barring any other details) it seems a little dodgy. I'm willing to bet composition may very well work better to manage those multiple instances.
But... if you are gonna do this, you can achieve it by intermediate inheritance. Can't have the same direct base appear more than once, but indirection is permissible.
template<int N>
struct ACopy : A {
using A::A;
};
class D: public ACopy<1>, public ACopy<2> {
};
Just go through the corresponding intermediate base for disambiguation purposes.
Alternatively (or additionally), the template ACopy
can have using declarations to make the protected members you care about into public ones*. That should facilitate the composition I suggested.
* - An often overlooked aspect of C is that "protected" is really "public with some extra steps required".