If I have something like
class Base {
static int staticVar;
}
class DerivedA : public Base {}
class DerivedB : public Base {}
Will both DerivedA
and DerivedB
share the same staticVar
or will they each get their own?
If I wanted them to each have their own, what would you recommend I do?
There is only one
staticVar
in your case:Base::staticVar
When you declare a static variable in a class, the variable is declared for that class alone. In your case, DerivedA can't even see
staticVar
(since it's private, not protected or public), so it doesn't even know there is astaticVar
variable in existence.