Are static variables in a base class shared by all derived classes?

前端 未结 7 1968
梦如初夏
梦如初夏 2020-11-28 04:56

If I have something like

class Base {
    static int staticVar;
}

class DerivedA : public Base {}
class DerivedB : public Base {}

Will bot

7条回答
  •  被撕碎了的回忆
    2020-11-28 05:56

    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 a staticVar variable in existence.

提交回复
热议问题