Static Class Variable Common to All Instances of Templated Class?

As the subject line says -- Is it possible to have a single (static) class variable that's common to all instances of a templated class?

The code below does not work like I want because the static 'instanceCounter' variable is only common to all instances of the same type 'T'. I'm looking for a static variable that's common to all instances of the 'TheClass' class across all types of 'T'.

Thanks.

template<typename T>
class TheClass {
  private:
    T internalValue;
    static uint8_t instanceCounter;

  public:
    TheClass(T v) : internalValue(v) {
      instanceCounter++;
    }

    void printValue() {
      Serial.print("internalValue = ");
      Serial.println(internalValue);
    }

    void printInstanceCountr() {
      Serial.printf("instanceCounter = %d\n", instanceCounter);
    }
};

template<>
uint8_t TheClass<uint8_t>::instanceCounter = 0;

template<>
uint8_t TheClass<const char *>::instanceCounter = 0;

TheClass<uint8_t> instance1(5);
TheClass<const char *> instance2("Hello World");
TheClass<uint8_t> instance3(100);

void setup() {
  Serial.begin(115200);
  delay(2000);

  instance1.printValue();
  instance1.printInstanceCountr();

  instance2.printValue();
  instance2.printInstanceCountr();

  instance3.printValue();
  instance3.printInstanceCountr();
}

void loop() {
}

Put a variable in the cpp file outside the class, then every object can use it.

Not what I'm looking for. It needs to be a static class variable, not a regular (free) variable.

This works. Not sure if there's a better way:

class MasterClass {
  protected:
    static uint8_t instanceCounter;
};

uint8_t MasterClass::instanceCounter = 0;

template<typename T>
class TheClass : public MasterClass {
  private:
    T internalValue;
    //static uint8_t instanceCounter;

  public:
    TheClass(T v) : internalValue(v) {
      instanceCounter++;
    }

    void printValue() {
      Serial.print("internalValue = ");
      Serial.println(internalValue);
    }

    void printInstanceCountr() {
      Serial.printf("instanceCounter = %d\n", instanceCounter);
    }
};

TheClass<uint8_t> instance1(5);
TheClass<const char *> instance2("Hello World");
TheClass<uint8_t> instance3(100);

void setup() {
  Serial.begin(115200);
  delay(2000);

  instance1.printValue();
  instance1.printInstanceCountr();

  instance2.printValue();
  instance2.printInstanceCountr();

  instance3.printValue();
  instance3.printInstanceCountr();
}

void loop() {
}
1 Like

I read the question, which went straight over my head, then I saw that @gfvalvo had provided the solution (which also went over my head) and I thought how nice it is to have ppl like @gfvalvo around who know stuff and contribute.

Finally I noticed it was @gfvalvo's question in the first place. How nice it is to know that everyone has things left to figure out. For themselves, sometimes.

a7

Thanks. Like I said, it's a solution. Not sure if it's the best solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.