Iterate through a list of class objects?

So I have a set of class objects and I want to run member functions on all of the class objects. I thought I would set up a for loop to do this. I know how to iterate through an enum list, but that does not work for this. What is the proper way to do this (instead of calling the member function over and over on separate lines?

Here is an example, where I want to run the ClearData member function on the four class members (Ref, Freez, Pump, and Count). (this is only illustrative since I know the for loop on the enum will put 1, 2, 3, and 4 into the value of s and not Ref, Freez, Pump, and Count)

Thanks for your thoughts on this.

#define NumOfBlocks 96

enum Sources {Begin, Ref, Freez, Pump, Count, End};

class Duty {
  private:
    uint8_t  m_Data[NumOfBlocks];
    float      m_DutyCycle;
    uint8_t  m_PortNumber;
    int         m_SumData;
  public:
    
    

    // constructor for instances that need port number
    Duty(uint8_t Port) : m_PortNumber(Port) {
    }

    // constructor without any inputs
    Duty() {
    }


    void ClearData() {
      for (uint8_t i = 0; i < NumOfBlocks; i++) {
        m_Data[i] = 0;
      }
    }
}; //class Duty

Duty Ref(RefIn);
Duty Freez(FreezIn);
Duty Pump();
Duty Count();


void setup() {
  for (Sources s = Sources::Begin; s != Sources::End; ++s) s.ClearData();  // (NOT EVEN CLOSE...)

}

void loop() {
}

Lots wrong here. Start with ..... you can't give class instances the same name as elements of an enum.

Also, 'RefIn' and 'FreezIn' are undefind.

After fixing that, you could make an array of class 'Duty' instances. Or, you could make an array of pointers to 'Duty' instances. Then, iterate through the array.

I’ll try the array approaches you suggested. Thank you.

And yes, the code is incomplete. I just snipped out enough to try to illustrate what I am trying to achieve.

Just a suggestion..

When writing class definitions. You may want to put the public stuff at the top. Because, when joe schmo wants to use your class later, Or even you! It would be nice to have the stuff he/you can access at the top. Instead of sifting through everything to find it at the bottom.

-jim lee

One way to iterate through a list of objects is to list them in an array:

constexpr int NumOfBlocks = 96;


class Duty
{
  private:


    uint8_t  m_Data[NumOfBlocks];
    float    m_DutyCycle;
    uint8_t  m_PortNumber;
    int      m_SumData;


  public:


    // constructor for instances that need port number
    Duty(uint8_t Port) : m_PortNumber(Port)
    {
    }


    // constructor without any inputs
    Duty()
    {
    }


    void ClearData()
    {
      for (uint8_t i = 0; i < NumOfBlocks; i++)
      {
        m_Data[i] = 0;
      }
    }
}; //class Duty


// Create the objects
Duty Begin;
Duty Ref(100);
Duty Freez(200);
Duty Pump;
Duty Count;
Duty End;


// List the objects
Duty *Sources[] = {&Begin, &Ref, &Freez, &Pump, &Count, &End};


void setup()
{
  for (size_t i = 0; i < sizeof Sources / sizeof Sources[0]; i++)
    Sources[i]->ClearData();
}


void loop()
{
}

Or, if you don’t actually need each of the instances to be explicitly named:

constexpr int NumOfBlocks = 96;

class Duty {
  private:
    uint8_t  m_Data[NumOfBlocks];
    float    m_DutyCycle;
    uint8_t  m_PortNumber;
    int      m_SumData;

  public:
    // constructor for instances that need port number
    Duty(uint8_t Port) : m_PortNumber(Port)    {
    }

    // constructor without any inputs
    Duty() : m_PortNumber(0) {  // don't leave m_PortNumber unitialized
    }

    void ClearData()    {
      for (uint8_t i = 0; i < NumOfBlocks; i++)
      {
        m_Data[i] = 0;
      }
      Serial.println(m_PortNumber);  // Just to show that we've entered the ClearData function
    }
}; //class Duty

// Create the objects
Duty Sources[] = {{}, 100, 200, {}, {}, {}};

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

  for (size_t i = 0; i < sizeof Sources / sizeof Sources[0]; i++) {
    Sources[i].ClearData();
  }
}

void loop() {
}

Another way to make objects iterate-able you could build them from something like a link-list object base class. Then its easy peasy.

-jim lee

johnwasser: One way to iterate through a list of objects is to list them in an array:

Thank you John. This was very helpful. One thing that was hanging my compilation was that, for the default constructors, I was using, for example Duty ref(); instead of just Duty ref; I am still getting into the Class semantics.