I have these C++ classes:
class Base
{
protected:
static int method()
{
static int x = 0;
return x++;
}
};
class A : public Base
There will only be one instance of x
in the entire program. A nice work-around is to use the CRTP:
template
class Base
{
protected:
static int method()
{
static int x = 0;
return x++;
}
};
class A : public Base { };
class B : public Base { };
This will create a different Base
, and therefore a distinct x
, for each class that derives from it.
You may also need a "Baser" base to retain polymorphism, as Neil and Akanksh point out.