Static variables in static method in base class and inheritance
问题 I have these C++ classes: class Base { protected: static int method() { static int x = 0; return x++; } }; class A : public Base { }; class B : public Base { }; Will the x static variable be shared among A and B , or will each one of them have it's own independent x variable (which is what I want)? 回答1: There will only be one instance of x in the entire program. A nice work-around is to use the CRTP: template <class Derived> class Base { protected: static int method() { static int x = 0;