I'm trying to get a static instance of an inherited class per base class. I.e, given the following code:
Code:
#include <stdio.h>

class A {
public:
  static int newIndex() {int i = index++; return i;}
private:
  static int index;
};

int A::index = 0;

class B : public A {
public:
 B() {}
};

class C : public A {
public:
 C() {}
};

int main(int argc, char **argv) {
  B *b = new B();
  C *c = new C();
  int bi = B::newIndex();
  int ci = C::newIndex();
  printf("bi = %i, ci = %i\n", bi, ci);
  return 0;
}
This code is compilable, and when runs it produces:
bi = 0, ci = 1

What I would like it to produce is:
bi = 0, ci = 0

That is, I would like to have a unique static instance of the "index" variable for every class that inherits A, instead of one global index variable which is the case at the moment. Is there anyway I can do this so that developers inheriting class A will get the desired functionality implictly (i.e. without using overkill software engineering techniques such as making the developer construct singleton classes and add them to each derived class instance)?

Thanks in advance,
Lukasz.