AとBの間で共有されると確信しています。
独立変数が必要な場合は、次のような「不思議なことに繰り返されるテンプレートパターン」を使用できます。
template<typename Derived>
class Base
{
protected:
static int method()
{
static int x = 0;
return x++;
}
};
class A : public Base<A>
{
};
class B : public Base<B>
{
};
もちろん、ポリモーフィズムが必要な場合は、次のようなものBase<A>
とは異なり、Baseが派生する「Baser」クラスを定義する必要があります。Base<B>
class Baser
{
};
template<typename Derived>
class Base : public Baser
{
protected:
static int method()
{
static int x = 0;
return x++;
}
};
class A : public Base<A>
{};
class B : public Base<B>
{};
これで、AとBも多型になります。