I have something like
struct Base{
int x=0;
};
And I want all the children to have a static variable "myint" so that, when built, they cumulate in x the values of myint for all parents and themselves. (In reality in my case x is a set and what i want is the union of the sets of each children).
struct Derived : public Base {
static int myint =1;
Derived(){x+=myint;}
};
struct Derived2 : public Derived {
static int myint = 2;
Derived2(){x+=myint;}
};
So that x=1 for Derived and x=3 for Derived2.
I've seen that something like this is possible with CRTP (static variable for each derived class) writing a BaseX class:
template<class A>
struct BaseX : public Base {
static int myint;
BaseX() {Base::x+=myint;}
}
struct Derived : public BaseX<Derived>;
But such pattern can't be applied for the second level of inheritance. I tried with multiple inheritance but what I obtain, of course, is that I have two values for x, each one with the wrong value (say x=1 for the part inheriting from Derived, and x=2 for the part deriving from BaseX).
Do you see any solution for this problem without having to call for (x+=myint) in all constructor and define myint in each derived?