/*
Forum: https://forum.arduino.cc/t/trying-to-assign-an-object-to-an-array-index/1424700
Wokwi: https://wokwi.com/projects/452879934535334913
ec2021
2026/01/12
Shows different possibilities to create arrays of objects
- as references to separate objects either in the declaration or as an assignment e.g. in setup()
- as independent instances in the array declaration by creating separate or copies of existing instances
2026/01/13
Added the declaration of directStandAloneArray and
further explanations for standAloneArray and directStandAloneArray
taken from the thread mentioned above
*/
constexpr char dblline[] = "================================";
class Dummy {
private:
byte value;
public:
Dummy(byte v): value(v) {};
byte getValue() {
return value;
};
void setValue(byte v) {
value = v;
};
};
// Create three instances separately
Dummy separate1(1),
separate2(2),
separate3(3);
// Create an Array using these instances (will create second instances of all three objects)
Dummy doubledArray[] = {
separate1,
separate2,
separate3
};
constexpr int noOfInstances = sizeof(doubledArray) / sizeof(doubledArray[0]);
// Create an Array of pointers to the separate objects by declaration
// Does not make a copy of the three instances
Dummy* ptrArrayDeclared[noOfInstances] = {
&separate1,
&separate2,
&separate3
};
// Create an Array of pointers but assign the addresses later (e.g. in setup())
// Does also not make a copy of the three instances
Dummy* ptrArrayAssigned[noOfInstances];
/*
Create an Array of individual instances, not related to other instances
Explanation from
https://forum.arduino.cc/t/trying-to-assign-an-object-to-an-array-index/1424700/30
Each element of the array is initialized from a temporary Dummy object
created by an explicit constructor call. For each element, a temporary
is constructed with the given argument, and that temporary is then used
to initialize the corresponding array element, conceptually via copy or
move construction. Copy elision may remove this step, but at the
language-rule level a temporary exists.
*/
Dummy standAloneArray[] = {
Dummy(5),
Dummy(6),
Dummy(7),
Dummy(99)
};
constexpr int noOfStandAlone = sizeof(standAloneArray) / sizeof(standAloneArray[0]);
/*
Create an Array of individual instances, not related to other instances, using curly brackets
Explanation from
https://forum.arduino.cc/t/trying-to-assign-an-object-to-an-array-index/1424700/30
Then each element of the array is directly list-initialized. The constructor of Dummy that
accepts an int is invoked directly for each array element, with no intermediate temporary
object in the abstract model.
*/
Dummy directStandAloneArray[] = {
{10},
{11},
{12}
};
constexpr int noOfDirectStandAlone = sizeof(directStandAloneArray) / sizeof(directStandAloneArray[0]);
void setup() {
Serial.begin(115200);
// Assign the object pointers to the array
ptrArrayAssigned[0] = &separate1;
ptrArrayAssigned[1] = &separate2;
ptrArrayAssigned[2] = &separate3;
printHeader("Start Values ");
printValues();
// Now we change the value of separate3!
separate3.setValue(0);
// Every call of getValue() that points to the instance separate3 should now show a zero
// Doubled instances will still show a 3 instead
printHeader("Values after separate3.setValue(0)");
printValues();
printHeader("Array of Stand Alone Objects");
printStandAlone();
printDirectStandAlone();
}
void loop() {
// In this sketch everything of relevance is done outside loop()
}
void printValues() {
printSeparateInstances();
printArrayDoubled();
printArrayDeclared();
printArrayAssigned();
}
void printSeparateInstances() {
Serial.println("Separate");
Serial.print(separate1.getValue());
Serial.print('\t');
Serial.print(separate2.getValue());
Serial.print('\t');
Serial.println(separate3.getValue());
}
void printArrayDoubled() {
Serial.println("doubledArray");
for (int i = 0; i < noOfInstances; i++) {
Serial.print(doubledArray[i].getValue());
Serial.print('\t');
}
Serial.println();
}
void printArrayDeclared() {
Serial.println("ptrArrayDeclared");
for (int i = 0; i < noOfInstances; i++) {
Serial.print(ptrArrayDeclared[i]->getValue());
Serial.print('\t');
}
Serial.println();
}
void printArrayAssigned() {
Serial.println("ptrArrayAssigned");
for (int i = 0; i < noOfInstances; i++) {
Serial.print(ptrArrayAssigned[i]->getValue());
Serial.print('\t');
}
Serial.println();
}
void printStandAlone() {
Serial.println("standAloneArray");
for (int i = 0; i < noOfStandAlone; i++) {
Serial.print(standAloneArray[i].getValue());
Serial.print('\t');
}
Serial.println();
}
void printDirectStandAlone() {
Serial.println("directStandAloneArray");
for (int i = 0; i < noOfDirectStandAlone; i++) {
Serial.print(directStandAloneArray[i].getValue());
Serial.print('\t');
}
Serial.println();
}
void printHeader(char *txt) {
Serial.println(dblline);
Serial.println(txt);
Serial.println(dblline);
}