Array of objects

lloyddean:
Well if we're goin to get fancy forget the above and let the new 'for' loop do the hard work of making the reference for you with -

for (auto display : displays)

{
    display.begin();
}




.. but this might be harder to explain, and, or, understand at his level of experience.

Although it does make it much easier to use the CURRENT element of the array within the loop!

That's incorrect.
Your code creates 4 copies of the original displays, begins each copy, then destroys the copies. The original displays are still in their same, uninitialized state.

You have to use references to the array elements returned by the iterator:

for (SevenSegmentExtended &display : displays) {
    display.begin();
}

or

for (auto &&display : displays) {
    display.begin();
}

if you're the lazy type.

Code demonstrating the problem:

#include <iostream>
using namespace std;

class C {
  public: 
    void increment() {
      c++;
    }
    int get() const {
      return c;
    }
  private:
   int c = 0;
};

template <size_t N> 
void print(const C (&arr)[N]) {
  for (const C &c : arr)
    cout << ' ' << c.get();
  cout << endl;
}

int main() {
  C arr[4];
  
  cout << "Initial" << endl;
  print(arr);

  for (C c : arr)
    c.increment();
  cout << "Incremented using `C c : arr`" << endl;
  print(arr);

  for (C &c : arr)
    c.increment();
  cout << "Incremented using `C &c : arr`" << endl;
  print(arr);

  for (auto &&c : arr)
    c.increment();
  cout << "Incremented using `auto &&c : arr`" << endl;
  print(arr);

  return 0;
}

Output:

Initial
 0 0 0 0
Incremented using `C c : arr`
 0 0 0 0
Incremented using `C &c : arr`
 1 1 1 1
Incremented using `auto &&c : arr`
 2 2 2 2

As you can see, using C c : arr does not increment the c value of the elements in the array.