Home > Mobile >  having to reassign the values of a array of objects to work in c /arduino
having to reassign the values of a array of objects to work in c /arduino

Time:01-05

i have code that have a global array of object, when i create it i already assign the objects, but before start using it on the code i have to reassign the values of the array on the function setup so i can access the objects inside it

the code that create the vector

HX711 CELL1;
HX711 CELL2;
HX711 CELL3;
HX711 CELL4;
HX711 celulas[] = {CELL1, CELL2, CELL3, CELL4};       // vector with the objects

the code on setup function that call the function that initialize the object, inside there isnt any new or anything, the begin is a normal function

  CELL1.begin(7, 8);
  celulas[0] = CELL1;
  CELL2.begin(5, 6);
  celulas[1] = CELL2;
  CELL3.begin(0, 1);
  celulas[2] = CELL3;
  CELL4.begin(A0, A1);
  celulas[3] = CELL4;

Can anyone explain what is going on ?

i expected that i can use the array after i created and assigned, not having to reassign the values of it

CodePudding user response:

You can of course use the array, but celulas[0] is not the same object as CELL1.

The line

HX711 celulas[] = {CELL1, CELL2, CELL3, CELL4};

copies the objects into the array - you now have eight HX711s, all independent from each other.

And, since your objects are default-initialized, it is almost equivalent to

HX711 celulas[4];

but with more copying.

In your setup, use the array elements directly:

celulas[0].begin(7, 8);
celulas[1].begin(5, 6);
celulas[2].begin(0, 1);
celulas[3].begin(A0, A1);
  • Related